在JUnit 4 Java中使用@BeforeClass和使用实例或静态变量有什么区别? [英] What's the difference between using @BeforeClass and using instance or static variable in JUnit 4 Java?

查看:375
本文介绍了在JUnit 4 Java中使用@BeforeClass和使用实例或静态变量有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是单元测试的新手.关于在 JUnit 4 中使用@Before注释的目的.我只是不知道使用它的目的:

I'm new to unit test. About the purpose of using @Before annotation in JUnit 4. I just don't know the point of using it:

public class FoodTestCase {
    static private Food sandwich;

    @BeforeClass
    public static void initialise(){
        sandwich = new Sandwich();    
    }

}

vs

public class FoodTestCase {
    static private Food sandwich = new Sandwich();

}

有什么区别?

推荐答案

我认为这个主意是这样的: 您使用@AfterClass释放资源.然后,有@BeforeClass来获取它们是合乎逻辑的.因为让开发人员猜测他需要使用静态块可能不是一个好主意.

I think the idea is like that: You use @AfterClass to free resources. Then it is logical to have @BeforeClass to acquire them. Because it may not be a good idea to let developer to guess that he need to use static block.

这篇关于在JUnit 4 Java中使用@BeforeClass和使用实例或静态变量有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆