以下面的类为例子:
@BeforeClass 和@AfterClass只执行一次,所以标注的方法也是static的
@Before和@After执行的次数跟你测试的方法的个数一样多,这里有testMethod1和testMethod2,所以会执行2次,
执行顺序:@BeforeClass->@Before->testMethod1-@After->@Before->testMethod2->@After->@AfterClass
public class JUnitTestSample{
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public final void testMethod1() {
}
@Test
public final void testMethod2() {
}
}
@BeforeClass 和@AfterClass只执行一次,所以标注的方法也是static的
@Before和@After执行的次数跟你测试的方法的个数一样多,这里有testMethod1和testMethod2,所以会执行2次,
执行顺序:@BeforeClass->@Before->testMethod1-@After->@Before->testMethod2->@After->@AfterClass