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

查看:26
本文介绍了实例变量的 Setter 和 Getter 的 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 测试中使用 get 方法,反之亦然 - 您不是在测试单个代码单元方法.

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.

鉴于...

假设我们有一个带有 value 字段的 PlainOldJavaObject,我们想要(出于某种原因)测试 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 和 getter,你将不得不做这样的代码(因为,如果你依赖于你的 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 和 Getter 的 Junit 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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