java setter 和 getter 的单元测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16797665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:01:21  来源:igfitidea点击:

unit testing of setters and getters

java

提问by avocadocadevra

Teacher wanted us to do a comprehensive unit test. For me, this will be the first time that I use JUnit. I am confused about testing set and get methods. Do you think should I test them? If the answer is yes; is this code enough for testing?

老师要我们做一个全面的单元测试。对我来说,这将是我第一次使用 JUnit。我对测试集和获取方法感到困惑。你认为我应该测试它们吗?如果答案是肯定的;这段代码足以进行测试吗?

  public void testSetandGet(){
    int a = 10;
    Class firstClass = new Class();
    firstClass.setValue(10);
    int value = firstClass.getValue();
    Assert.assertTrue("Error", value==a);
  }

In my code, I think if there is an error, we can't know that the error is deriving because of setter or getter.

在我的代码中,我认为如果出现错误,我们无法知道错误是由 setter 或 getter 引起的。

回答by cyon

I would say don't write unit tests for setters and getters, unless your setters and getters contain program logic (something getting computed as the setter or getter is called).

我会说不要为 setter 和 getter 编写单元测试,除非你的 setter 和 getter 包含程序逻辑(在调用 setter 或 getter 时计算的东西)。

On a real project you will have more than plenty program logic to unit test without having to test something trivial like setters and getters.

在实际项目中,您将拥有大量的程序逻辑来进行单元测试,而无需测试诸如 setter 和 getter 之类的琐碎内容。

回答by avocadocadevra

Roy Osherove in his famous book 'The Art Of Unit Testing' says:

Roy Osherove 在他的著名著作《单元测试的艺术》中说:

Properties (getters/setters in Java) are good examples of code that usually doesn't contain any logic, and doesn't require testing. But watch out: once you add any check inside the property, you'll want to make sure that logic is being tested.

属性(Java 中的 getter/setter)是通常不包含任何逻辑且不需要测试的代码的好例子。但要注意:一旦您在属性中添加任何检查,您将需要确保正在测试逻辑。

You are testing what you set, can also be "get". Change your Assertstatement to:

您正在测试您设置的内容,也可以“获取”。将您的Assert声明更改为:

Assert.assertTrue(a, true);

Assert.assertTrue(a, true);

Also, you should pass a, if you are going to use it.

此外,a如果您要使用它,您应该通过。

回答by Leep

I think it is important if you have some conditions in it. For exemple if it return an exception, if your int must be positive or some other conditions. But if it is just a variable assignation, I don't think it is useful... It is the same for getters.

我认为这很重要,如果你有一些条件。例如,如果它返回异常,如果您的 int 必须为正数或其他一些条件。但是如果只是变量赋值,我觉得没什么用……对于getter也是一样。

回答by Konstantin Yovkov

If I were you, I would first setsome value to a property in the class I'm testing, and then I will make sure that the getmethod returns the same value.

如果我是你,我会首先set为我正在测试的类中的属性设置一些值,然后我将确保该get方法返回相同的值。

public void testSetandGet(){
    int a = 10;
    Class firstClass = new Class();
    firstClass.setValue(10);
    int value = firstClass.getValue();
    Assert.assertEquals(value, a);
}

回答by PaulJWilliams

Dont unit test anything that relies on things outside of the class under test to work.

不要单元测试任何依赖于被测类之外的东西才能工作的东西。

If your getters and setters are trivial then they can only fail if the underlying JVM/compiler fails - so you're actually testing the JVM/compiler, not your code.

如果你的 getter 和 setter 是微不足道的,那么它们只会在底层 JVM/编译器失败时失败——所以你实际上是在测试 JVM/编译器,而不是你的代码。

回答by user1646196

If you wanted to test it you could temporarily make the variable public and then:

如果你想测试它,你可以暂时公开变量,然后:

firstClass.setMyInt(5);
Assert.assertTrue(5, firstClass.myInt);

However getters and setters can be automatically generated, they typically only contain one line in the method and if they cause your code to fail you probably have bigger problems. It's not common practise with unit testing to bother testing the getters and setters and you can avoid testing them and your unit testing will still be comprehensive.

然而 getter 和 setter 可以自动生成,它们通常在方法中只包含一行,如果它们导致您的代码失败,您可能会遇到更大的问题。单元测试的常见做法是费心测试 getter 和 setter,您可以避免测试它们,并且您的单元测试仍然是全面的。