Java 如何在 jUnit 中测试一个非常简单的 void 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20612946/
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-08-13 03:07:20  来源:igfitidea点击:

How do I test a very simple void method in jUnit?

javajunit

提问by

I know there are several question about void-method Unit-Testing, but my question is different.
I'm learning java, so my boss give me some tasks with different requirements on my tasks.

我知道有几个关于无效方法单元测试的问题,但我的问题是不同的。
我正在学习java,所以我的老板给了我一些对我的任务有不同要求的任务。

In my actual task, there is a requirement which says, the jUnit test must cover >60%. So I need to test a very simple method to reach this 60%. The method is the following:

在我的实际任务中,有一个要求是,jUnit 测试必须覆盖 >60%。所以我需要测试一个非常简单的方法来达到这个 60%。方法如下:

public void updateGreen() {
    // delete this outprint if the Power Manager works
    System.out.println(onCommand + "-green");
    // p = Runtime.getRuntime().exec(command + "-green");
    // wait until the command is finished
    // p.waitFor();
}

Because of intern problems, I can't execute the command with the Runtimetask. So there is only a System.outin this method.

由于实习生问题,我无法执行带有Runtime任务的命令。所以System.out这个方法中只有一个。

I've multiple methods like that, so tests for this method will cover over 10% of my whole code.

我有多种类似的方法,因此对这种方法的测试将覆盖我整个代码的 10% 以上。

Is it useful to test such a method? When yes, how?

测试这样的方法有用吗?什么时候是,如何?

采纳答案by Andrey Chaschev

If there is a lot of such methods, the thing which you might want to test here is that updateScreen()uses the right string, "some-command-green" and that the System.outis being invoked. In order to do this you might want to extract System.outinto an object field and mock it (i.e. with Mockito's spy()) to test the string that was provided to println.

如果有很多这样的方法,您可能想在这里测试的是updateScreen()使用正确的字符串“some-command-green”并且System.out正在调用的东西。为了做到这一点,您可能想要提取System.out到一个对象字段中并模拟它(即使用 Mockito 的spy())来测试提供给 的字符串println

I.e.

IE

class MyClass{
     PrintStream out = System.out;

     public void updateGreen() { ... }
}

In test:

测试中:

@Test
public void testUpdate(){
     MyClass myClass = new MyClass();
     myClass.out = Mockito.spy(new PrintStream(...));

     // mock a call with an expected input
     doNothing().when(myClass.out).println("expected command");

     myClass.updateGreen();

     // test that there was a call
     Mockito.verify(myClass.out, Mockito.times(1)).println("expected command");
}

回答by Husman

You could return true if the method ran successfully and false otherwise. It would be easy to test for this.

如果方法成功运行,您可以返回 true,否则返回 false。对此进行测试会很容易。

You could also test the output of this method, as described here: Should we unit test console outputs?

您还可以测试此方法的输出,如下所述: 我们应该对控制台输出进行单元测试吗?

But in my experience, it is much better to have methods return an optimistic or pessimistic value (true/false, 1/0/-1 etc) to indicate their status.

但根据我的经验,让方法返回一个乐观或悲观的值(真/假、1/0/-1 等)来指示它们的状态要好得多。

You can also write a getter method for the onCommand flag:

您还可以为 onCommand 标志编写一个 getter 方法:

public string getFlag(){
  // some logic here
  return "green";

  // otherwise default to no flags
  return "";
}

回答by Stefan Birkner

You could test that onCommand + "-green"has been written to System.outby using the System Rules library.

您可以使用系统规则库测试onCommand + "-green"已写入System.out内容。