实例变量的Setter和getters的Junit测试 [英] Junit Test of Setters and Getters of Instance Variables

查看:557
本文介绍了实例变量的Setter和getters的Junit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为对象内的实例变量的setter和getter创建测试用例。什么是最好的方法?在这里,我在我的测试中使用get和set方法。这是不好的测试策略吗?

When creating test cases for setters and getters of instance variables within the object. What is the best approach? Here I use the get and set methods within my tests. Would this be poor testing strategy?

/**
 * Test of setFlightNumber method, of class Flight.
 */
@Test
public void testSetFlightNumber() {
    System.out.println("setFlightNumber");
    int flightNumber = 1000;
    Flight instance = new Flight();
    instance.setFlightNumber(flightNumber);
    // TODO review the generated test code and remove the default call to fail.
    assertEquals(instance.getFlightNumber(), flightNumber);
}

/**
 * Test of getFlightNumber method, of class Flight.
 */
@Test
public void testGetFlightNumber() {
    System.out.println("getFlightNumber");
    Flight instance = new Flight();
    int expResult = 1000;
    instance.setFlightNumber(1000);
    int result = instance.getFlightNumber();
    assertEquals(expResult, result);
}


推荐答案

单元测试的主要原则是你测试一个简单的单位代码;也就是说,每种方法都应该按照自己的优点进行测试。

The main principle of unit testing is that you test a simple unit of code; that is, each method should be tested on its own merits.

这意味着我们不能使用获取我们的 set 测试中的方法,反之亦然 - 您没有测试单个方法的单个代码单元。

This means we can't use the get method in our set test, and vice versa - you're not testing the individual unit of code that is the single method.

鉴于......

假设我们有一个 PlainOldJavaObject ,字段 value 我们想要(出于某种原因)测试setter和getter的有效性。唯一合适的方法是通过使用反射

Let's say we have a PlainOldJavaObject with a field value that we want (for some reason) to test the validity of setters and getters for. The only appropriate way to do this is through the use of reflection.

这是我的类声明,非常瘦:

Here's my class declaration, which is pretty skinny:

public class PlainOldJavaObject {

    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

我现在设置我的测试类来使用反射;特别是使用 Field 类:

I now set up my test class to make use of reflection; specifically using the Field class:

public class PlainOldJavaObjectTest {

    @Test
    public void testSetter_setsProperly() throws NoSuchFieldException, IllegalAccessException {
        //given
        final PlainOldJavaObject pojo = new PlainOldJavaObject();

        //when
        pojo.setValue("foo");

        //then
        final Field field = pojo.getClass().getDeclaredField("value");
        field.setAccessible(true);
        assertEquals("Fields didn't match", field.get(pojo), "foo");
    }

    @Test
    public void testGetter_getsValue() throws NoSuchFieldException, IllegalAccessException {
        //given
        final PlainOldJavaObject pojo = new PlainOldJavaObject();
        final Field field = pojo.getClass().getDeclaredField("value");
        field.setAccessible(true);
        field.set(pojo, "magic_values");

        //when
        final String result = pojo.getValue();

        //then
        assertEquals("field wasn't retrieved properly", result, "magic_values");
    }

}

在第一次测试中,我是确保通过反射访问 PlainOldJavaObject 实例的字段中包含的值来读取字段。在没有违反声明的类*的完整性的情况下,我确信该字段设置正确。

In the first test, I am making certain that the field is read by reflectively accessing the value contained in the field for the instance of PlainOldJavaObject. Without violating the integrity of the declared class*, I am confident that the field is being set appropriately.

在第二个测试中,我假设该值已经设置事先确定,所以设置涉及使用已知的默认值填充字段。当我读回值时,我断言读回的值是我们知道它最初设置的值。

In the second test, I assume that the value has already been set to something prior, so the set-up involves populating the field with a known default value. When I read the value back, I am asserting that the value read back is the value that we know it was originally set to.

最终,如果你有很多setter和getters,你将不得不做这样的代码(因为,如果你依赖于你的setter和getter正常工作的假设,就像你在上面的测试中那样,你的测试用例可能无效)。

Ultimately, if you have a lot of setters and getters, you'll have to do code like this (since, if you rely on the assumption that your setters and getters "just work", like you are in your tests above, your test cases may be invalid).

*:请注意,反射是一种快速而快速的方法,可以通过未定义的行为陷入极端麻烦,并且几乎没有保证对象不变性。你正在采用语言的收缩包装,做着奇怪而不寻常的事情。继续自担风险。

这篇关于实例变量的Setter和getters的Junit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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