如何对构造函数进行单元测试 [英] How to unit test constructors

查看:420
本文介绍了如何对构造函数进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂课要添加单元测试.该类具有多个构造函数,这些构造函数采用不同的类型并将其转换为规范形式,然后可以将其转换为其他类型.

I have a class I am adding unit tests to. The class has several constructors which take different types and converts them into a canonical form, which can then be converted into other types.

public class Money {
    public Money(long l) {
        this.value = l;
    }

    public Money(String s) {
        this.value = toLong(s);
    }

    public long getLong() {
        return this.value;
    }

    public String getString() {
        return toString(this.value);
    }
}

实际上,它接受并转换为其他两种类型.

In reality there are a couple of other types it accepts and converts to.

我正在尝试找出最合适的方法来测试这些构造函数.

I am trying to work out what the most appropriate way to test these constructors is.

是否应针对每个构造函数和输出类型进行测试:

Should there be a test per-constructor and output type:

@Test
public void longConstructor_getLong_MatchesValuePassedToConstructor() {
    final long value = 1.00l;

    Money m = new Money(value);
    long result = m.getLong();

    assertEquals(value, result);
}

这导致许多不同的测试.如您所见,我正在努力命名它们.

This leads to a lot of different tests. As you can see, I'm struggling to name them.

应该有多个断言:

@Test
public void longConstructor_outputsMatchValuePassedToConstructor() {
    final long longValue = 1.00l;
    final String stringResult = "1.00";

    Money m = new Money(longValue);

    assertEquals(longValue, m.getLong());
    assertEquals(stringResult, m.getString());
}

这有多个断言,这让我感到不舒服.它还正在测试getString(并通过代理toString),但未在测试名称中说明.命名这些更加困难.

This has multiple asserts, which makes me uncomfortable. It is also testing getString (and by proxy toString) but not stating that in the test name. Naming these are even harder.

我专注于构造函数是完全错误的.我应该只测试转换方法吗?但是随后的测试将错过toLong方法.

Am I going about it completely wrong by focussing on the constructors. Should I just test the conversion methods? But then the following test will miss the toLong method.

@Test
public void getString_MatchesValuePassedToConstructor() {
    final long value = 1.00;
    final String expectedResult = "1.00";

    Money m = new Money(value);
    String result = m.getLong();
    assertEquals(expectedResult, result);
}

这是一个遗留类,我无法更改原始类.

This is a legacy class and I can't change the original class.

推荐答案

看起来您已经获得了一种规范的方法来获取原始"值(在本例中为toLong)-因此只需测试所有构造函数获取那个值时是正确的.然后,您可以基于单个构造函数测试其他方法(例如getString()),因为您知道一旦各种构造函数完成,它们都会使对象保持相同的状态.

It looks like you've got a canonical way of getting the "raw" value (toLong in this case) - so just test that all the constructors are correct when you fetch that value. Then you can test other methods (such as getString()) based on a single constructor, as you know that once the various constructors have finished, they all leave the object in the same state.

这是假设有些白盒测试-即您知道 toLong实际上是对内部状态的简单反映,因此可以在测试中测试该+构造函数.

This is assuming somewhat white-box testing - i.e. you know that toLong is really a simple reflection of the internal state, so it's okay to test that + a constructor in a test.

这篇关于如何对构造函数进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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