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

查看:18
本文介绍了最佳实践:在 setUp() 或 at 声明中初始化 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 常见问题解答中的示例,例如 基本测试模板,我认为展示的最佳实践是被测类应该在你的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 或测试方法中创建您的对象,以便您在以后破坏它时能够获得合理的输出.

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() 或 at 声明中初始化 JUnit 类字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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