junit4施用总结

junit4使用总结
junit4注解标记的方法运行的顺序
@BeforeClass->@Before->@Test->@After->@AfterClass

如果需要在开始之前执行某个方法需要使用该注解标注,主要用来初始化资源
@BeforeClass

如果需要在每个测试方法执行前都执行一次需要使用该注解标注,主要用来初始化资源
@Before

每个测试的方法需要使用该注解标注,主要要来运行测试方法
@Test
该注解还有其它的一些属性,如excepted(预计有异常),time(限定执行时间),等

相对应上面的注解@After,@AfterClass分别是在方法和类执行完之后,都会执行一次.主要用于回收资源。

注意:
1.使用上面注解标记的方法名称都需要以test开头。
2.类声明的成员变量,如果需要在@Before,@BeforeClass实例化时,一定要声明为静态的。
  如:static String s = "123";否则在@Test方法中,获取该实例对象时为Null.

其它常用的注解:
如果某个测试方法暂时不参与测试,可以将@Test改为@Ignore,即可。
@Ignore

样例:
public class IApplyServiceTest extends SpringService {
static IApplyService applyservice;
@BeforeClass
public void testInit() {
applyservice = super.getIapplyService();
System.out.println(applyservice);
}

@Test
public void testMySearcher() {
Assert.notNull(applyservice);
MySearcher mysearcher =  applyservice.getSearcherByUserId(1);
Assert.notNull(mysearcher);
}
}
1 楼 xurichusheng 2012-06-04  
再有个具体的例子就好了
2 楼 lusanxiong 2012-06-05  
xurichusheng 写道
再有个具体的例子就好了

加了个简单的样例。。。