`

junit复习1 @before class @after class

 
阅读更多
先看代码
  

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 对于那些比较“昂贵”的资源的分配或者释放来说是很有效的
分享到:
评论

相关推荐

    junit的jar包

    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....

    MyBatis 需要注意的地方junit注解

    @RunWith 指定测试类使用的某个运行器参数SpringJUnit4ClassRunner.class @Parameters 指定参数类的参数数据集合 @Rule 允许灵活添加或重新定义测试类中的每个测试方法的行为 @FixMethodOrder 指定测试方法的执行...

    Junit4Demo:Junit4 演示

    junit4 使用总结 @Test @Test(timeout=毫秒),@Test(expected=异常类) @Ignore ...@BeforeCalss @AfterClass @Before @After 测试套件@RunWith(Suite.class) 参数化设置@RunWith(Parameterized.class)

    Android代码-okir

    @RunWith(AndroidJunit.class) public class MyEspressoTest { private OkHttp3IdlingResource okir = new OkHttp3IdlingResource(); } register it on Espresso, @Before public void setUp() { Espresso....

    百度地图毕业设计源码-JavaWebPractice:展示了我的javaweb学习过程

    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-测试:不部署到生产-包名称...

    Spring AOP配置源码

    public void test1(){ userService.say(); System.out.println(); userService.smile(); System.out.println(); userService.cry(); } } 此单元测试基于spring的AbstractJUnit4SpringContextTests,你需要...

    day020-继承加强和设计模式代码和笔记.rar

    1. 单元测试:(掌握) 1. 写一个单元测试类,命名方式:XxxTest(测试类没有main方法) 2. 导入包,Junit4包 选中项目,右键 => Build Path => Add Library => 选中Junit 选中Junit4 => finish 3. 在...

    spectre:基于Rspec的Java 8测试框架

    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....

    spring-framework-reference4.1.4

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

    spring-framework-reference-4.1.2

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

    Use Case Driven Object Modeling with UML_ Theory and Practice.pdf

    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. . . . . . . . . . . . ....

Global site tag (gtag.js) - Google Analytics