---
title: "MyEclipse下JUnit的简单例子"
author: "GreatNeyo"
date: "2010-06-12T11:09:09Z"
tags: ["Myeclipse", "junit"]
categories: ["编程语言"]
slug: "myeclipse-junit"
draft: false
lastmod: "2010-06-12T11:22:13Z"
id: 690364
url: "https://greatneyo.iteye.com/blog/690364"
---

在MyEclipse下使用Junit，以一个最简单的例子来记录整个过程
一、创建一个待测试类JunitDemo

```java

/*
 * 测试使用Junit
 */
public class JunitDemo {
	public static void main(String[] args) {	
	}
	public String returnstring(){
		return "I'm good";
	}
}


```

二、鼠标右击待测试类，加入“JUnit Test Case”
![](http://dl.iteye.com/upload/attachment/263628/ae90736a-464a-341b-b5b0-d2abb009bf4a.jpg)
新建测试类TestJunitDemo
![](http://dl.iteye.com/upload/attachment/263630/7994b660-0d0b-3de0-9df5-87243f90a240.jpg)
三、写测试类实现代码

```java

import junit.framework.TestCase;

public class TestJunitDemo extends TestCase {

	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testReturnstring() {
		JunitDemo jd = new JunitDemo();
		this.assertEquals("I'm good", jd.returnstring());
	}

}

```

四、运行Run As ->JUnit Test，
在JUnit测试平台中，绿色表示测试通过，红色表示未通过，如下图：
![](http://dl.iteye.com/upload/attachment/263635/1bf08a13-1528-3d65-a8fa-0c6ba9b8acdf.jpg)