最佳实践:在setUp()或声明中初始化JUnit类字段? [英] Best Practice: Initialize JUnit class fields in setUp() or at declaration?

查看:88
本文介绍了最佳实践:在setUp()或声明中初始化JUnit类字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该在这样的声明中初始化类字段吗?

Should I initialize class fields at declaration like this?

public class SomeTest extends TestCase
{
    private final List list = new ArrayList();

    public void testPopulateList()
    {
        // Add stuff to the list
        // Assert the list contains what I expect
    }
}

或者像这样的setUp()?

Or in setUp() like this?

public class SomeTest extends TestCase
{
    private List list;

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        this.list = new ArrayList();
    }

    public void testPopulateList()
    {
        // Add stuff to the list
        // Assert the list contains what I expect
    }
}

我倾向于使用第一种形式,因为它更简洁,并允许我用最后的字段。如果我不需要使用setUp()方法进行设置,我是否还应该使用它,为什么?

I tend to use the first form because it's more concise, and allows me to use final fields. If I don't need to use the setUp() method for set-up, should I still use it, and why?

澄清:
JUnit将根据每种测试方法实例化一次测试类。这意味着每次测试都会创建 list ,无论我在哪里声明它。这也意味着测试之间没有时间依赖关系。因此,使用setUp()似乎没有任何优势。然而,JUnit FAQ有很多例子在setUp()中初始化一个空集合,所以我认为必须有一个原因。

Clarification: JUnit will instantiate the test class once per test method. That means list will be created once per test, regardless of where I declare it. It also means there are no temporal dependencies between the tests. So it seems like there are no advantages to using setUp(). However the JUnit FAQ has many examples that initialize an empty collection in setUp(), so I figure there must be a reason.

推荐答案

如果您特别想知道JUnit FAQ中的示例,例如 basic测试模板,我认为最好的做法是在测试中应该在你的setUp方法(或测试方法)中实例化。

If you're wondering specifically about the examples in the JUnit FAQ, such as the basic test template, I think the best practice being shown off there is that the class under test should be instantiated in your setUp method (or in a test method).

当JUnit示例在setUp方法中创建一个ArrayList时,它们都会继续测试该ArrayList的行为,例如testIndexOutOfBoundException,testEmptyCollection等。从某种角度来看,有人在编写一个类并确保它正常工作。

When the JUnit examples create an ArrayList in the setUp method, they all go on to test the behavior of that ArrayList, with cases like testIndexOutOfBoundException, testEmptyCollection, and the like. The perspective there is of someone writing a class and making sure it works right.

在测试自己的类时,你应该这样做:在setUp或者在setUp中创建你的对象如果你以后破解它,你将能够获得合理的输出。

You should probably do the same when testing your own classes: create your object in setUp or in a test method, so that you'll be able to get reasonable output if you break it later.

另一方面,如果你使用Java集合类(或者在你的测试代码中,其他库类,可能不是因为你想测试它 - 它只是测试夹具的一部分。在这种情况下,您可以安全地假设它按预期工作,因此在声明中初始化它不会是一个问题。

On the other hand, if you use a Java collection class (or other library class, for that matter) in your test code, it's probably not because you want to test it--it's just part of the test fixture. In this case, you can safely assume it works as intended, so initializing it in the declaration won't be a problem.

为了它的价值,我在相当大,几年,TDD开发的代码库。我们习惯性地在测试代码的声明中初始化事物,并且在我参与这个项目的一年半中,它从未引起过任何问题。所以至少有一些轶事证据表明这是合理的事情。

For what it's worth, I work on a reasonably large, several-year-old, TDD-developed code base. We habitually initialize things in their declarations in test code, and in the year and a half that I've been on this project, it has never caused a problem. So there's at least some anecdotal evidence that it's a reasonable thing to do.

这篇关于最佳实践:在setUp()或声明中初始化JUnit类字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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