先看代码
public class JunitTest1 {
private Collection collection;
@BeforeClass
public static void oneTimeSetUp() {
// one-time initialization code
System.out.println("@BeforeClass - oneTimeSetUp");
}
@AfterClass
public static void oneTimeTearDown() {
// one-time cleanup code
System.out.println("@AfterClass - oneTimeTearDown");
}
@Before
public void setUp() {
collection = new ArrayList();
System.out.println("@Before - setUp");
}
@After
public void tearDown() {
collection.clear();
System.out.println("@After - tearDown");
}
@Test
public void testEmptyCollection() {
assertTrue(collection.isEmpty());
System.out.println("@Test - testEmptyCollection");
}
@Test
public void testOneItemCollection() {
collection.add("itemA");
assertEquals(1, collection.size());
System.out.println("@Test - testOneItemCollection");
}
}
其中要注意的是 @BeforeClass和 @afterclass要在静态方法中使用,它们的含义为 :
1) 在一个类中只可以出现一次
2) @BeforeClass父类中标识了该Annotation的方法将会先于当前类中标识了该Annotation的方法执行。
@AfterClass 父类中标识了该Annotation的方法将会在当前类中标识了该Annotation的方法之后执行
3) 必须声明为public static
4) 所有标识为@AfterClass的方法都一定会被执行,即使在标识为@BeforeClass的方法抛出异常的的情况下也一样会
5) @BeforeClass 和 @AfterClass 对于那些比较“昂贵”的资源的分配或者释放来说是很有效的
分享到:
相关推荐
org.junit.After.class org.junit.AfterClass.class org.junit.Assert.class org.junit.Assume.class org.junit.Before.class org.junit.BeforeClass.class org.junit.ComparisonFailure.class org.junit.Ignore....
@RunWith 指定测试类使用的某个运行器参数SpringJUnit4ClassRunner.class @Parameters 指定参数类的参数数据集合 @Rule 允许灵活添加或重新定义测试类中的每个测试方法的行为 @FixMethodOrder 指定测试方法的执行...
junit4 使用总结 @Test @Test(timeout=毫秒),@Test(expected=异常类) @Ignore ...@BeforeCalss @AfterClass @Before @After 测试套件@RunWith(Suite.class) 参数化设置@RunWith(Parameterized.class)
@RunWith(AndroidJunit.class) public class MyEspressoTest { private OkHttp3IdlingResource okir = new OkHttp3IdlingResource(); } register it on Espresso, @Before public void setUp() { Espresso....
class CaculatorTest { @Before public void inite(){ System.out.println("Before方法执行了"); } @After public void close(){ System.out.println("After方法被执行了"); } @Test public void addTest(){ //1、...
测试包括:测试用例:@Test批注@ BeforeClass,@ Before,@ After,@ AfterClass,用于测试其行为 注意:项目结构 项目-src:部署到生产-包名称,例如com.junit.class-类名,例如MyClass-测试:不部署到生产-包名称...
public void test1(){ userService.say(); System.out.println(); userService.smile(); System.out.println(); userService.cry(); } } 此单元测试基于spring的AbstractJUnit4SpringContextTests,你需要...
1. 单元测试:(掌握) 1. 写一个单元测试类,命名方式:XxxTest(测试类没有main方法) 2. 导入包,Junit4包 选中项目,右键 => Build Path => Add Library => 选中Junit 选中Junit4 => finish 3. 在...
import static org.eck.spec.Spectre.before ; import static org.eck.spec.Spectre.after ; import static org.eck.spec.Spectre.it ; import java.util.ArrayList ; import java.util.List ; import org.junit....
Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................
Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................
Do an Initial Domain Model Before You Write the Use Cases. . . . . . 50 Driving Your Design (and Your Tests) from the Use Cases. . . . . . . . . 51 Use Case Modeling in Theory. . . . . . . . . . . . ....