Junit注解修饰的方法必须是 public 、void、无参的测试方法
1. 前5大注解测试
public class Test1 {
@BeforeClass
public static void beforeClazz() {
System.out.println("beforeClazz" );
}
@Before
public void before() {
System.out.println("before");
}
@Test
public void test1() {
System.out.println("test" + this);
}
@Test
public void test2() {
System.out.println("test2" + this);
}
@After
public void after() {
System.out.println("after");
}
@AfterClass
public static void afterClazz() {
System.out.println("afterClazz");
}
}
运行结果
2. @Test
当测试类中没有一个@Test修饰的方法,则单元测试类不可以被启动
3. @Ignore
即使修饰了所有@Test方法,但仍然能启动单元测试类
修饰所有@Test - 依然能运行@BeforeClass、@AfterClass注解的静态方法
public class Test1 {
@BeforeClass
public static void beforeClazz() {
System.out.println("beforeClazz" );
}
@Before
public void before() {
System.out.println("before");
}
@Test
@Ignore
public void test1() {
System.out.println("test" + this);
}
@Test
@Ignore
public void test2() {
System.out.println("test2" + this);
}
@After
public void after() {
System.out.println("after");
}
@AfterClass
public static void afterClazz() {
System.out.println("afterClazz");
}
}
运行结果
修饰整个测试类,所有测试注解修饰的方法都不能运行
@Ignore
public class Test1 {
@BeforeClass
public static void beforeClazz() {
System.out.println("beforeClazz" );
}
@Before
public void before() {
System.out.println("before");
}
@Test
public void test1() {
System.out.println("test" + this);
}
@Test
public void test2() {
System.out.println("test2" + this);
}
@After
public void after() {
System.out.println("after");
}
@AfterClass
public static void afterClazz() {
System.out.println("afterClazz");
}
}
运行结果