•
1.要先导入
Junit4
的包:
junit-4.4.jar
•
2.JUnit
不要求必须继承自
TestCase
,
•
3.测试方法必须使用注解
org.junit.Test
修饰
@Test
•
4.测试方法必须使用
public void
修饰,而且不能带有任何参数。
•
5.测试方法可不必以
test
开头
•
6.可利用
@Before
注解的方法生成测试方法中所需要的对象
•
列如:
public class AddOperationTest {
public AddOperationTest() { }
public AddOperationTest() { }
AddOperation instance;
@Before
public void getObj() {
public void getObj() {
instance = new AddOperation();
}
@After
public void tearDown() {
… }
}
@After
public void tearDown() {
… }
@Test
public void add() {
System.out.println(/"add/");
int x = 0;
int y = 0;
int expResult = 0;
int result = instance .add(x, y);
assertEquals(expResult, result);
} }
public void add() {
System.out.println(/"add/");
int x = 0;
int y = 0;
int expResult = 0;
int result = instance .add(x, y);
assertEquals(expResult, result);
} }