1、常用的注解,代码示例:
package test.demo;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.BeforeClass;
import org.junit.Test;
public class JunitDemo {
private String str;
@BeforeClass //使用该注解的方法,表示在实例化整个类之前执行一次
public static void beforeClass(){
}
@AfterClass //使用该注解的方法,表示在实例化整个类之后执行一次
public static void afterClass(){
}
@Before //使用该注解的方法,表示在执行Test方法之前执行一次
public void setUp(){
}
@After //使用该注解的方法,表示在执行Test方法之后执行一次
public void tearDown(){
}
@Test //使用该注解的方法,表示要执行的测试方法
public void test(){}
@Ignore("this test has not implemented ")
@Test //使用Ignore表示此测试方法忽略不执行,也可以指定string消息,指示原因
public void hello(){}
@Test(expected=MyException.class) //Test中的expected选项是期望有一个MyException异常抛出,如果没有抛出执行失败
public void Demo() throws MyException{
throw new MyException("hello exception");
}
@Test(timeout=5000) //Test中timeout选项是限时执行,以毫秒为单位,如果超出还没有执行完则报timeout错误
public void timeout(){
int i=0;
while(i < 3){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
System.out.println(str);
}
}
}
class MyException extends Exception{
private static final long serialVersionUID = 1L;
super(message);
}
}