Java 测试返回类型为 void 的方法

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

Testing a method with return type void

javatestingjunitmockito

提问by user1170330

I'm new to software testing. In a tutorial I read that I can test some methods like this:

我是软件测试的新手。在一个教程中,我读到我可以测试一些这样的方法:

@Test
 public void testMultiply() {
   MyClass tester = new MyClass();
   assertEquals("10 x 5 must be 50", 50, tester.multiply(10, 5));
 } 

So I have to pass some parameters to a method and get a return value.
But in my case I have some classes without any parameters and return values:

所以我必须将一些参数传递给一个方法并获得一个返回值。
但就我而言,我有一些没有任何参数和返回值的类:

ArrayList al = new ArrayList();

public void Main(){
  methodA();
  methodB();
}

public void methodA(){
  // connect to URL
  // get some data
  for(int i=0; i<someThing; i++){
    al.add(data);
  }
}
public void methodB(){
  // do something with al
}

Is it also possible to test them? For example whether the right value comes from the URL in methodA().

是否也可以测试它们?例如,正确的值是否来自methodA().

采纳答案by rgettman

It is possible to test methods that don't return anything (void), but you must test that method's side effects. It should have some side effects, otherwise such a method is useless.

可以测试不返回任何内容 ( void) 的方法,但您必须测试该方法的副作用。它应该有一些副作用,否则这种方法是没有用的。

The side effect here is that alis changed. As written, this class doesn't have any other methods that expose al. Add a getter method that returns either the ArrayListor a specific element of the ArrayList. Then your test method can call the getter method to retrieve the value(s) and compare it(them) with the expected value(s).

这里的副作用al是改变了。正如所写,这个类没有任何其他公开al. 添加的吸气剂方法,该方法返回任一ArrayList或的一个特定的元素ArrayList。然后您的测试方法可以调用 getter 方法来检索值并将其与预期值进行比较。

回答by dasblinkenlight

Methods without parameters are used for their return value or for their side effects. Therefore, testing of these methods consists of these three steps:

没有参数的方法用于它们的返回值或它们的副作用。因此,这些方法的测试包括以下三个步骤:

  • Set the object being tested to the initial state before calling the parameterless method
  • Call the parameterless method
  • Check the return value or the side effects of the parameterless method
  • 在调用无参数方法之前将被测对象设置为初始状态
  • 调用无参数方法
  • 检查无参数方法的返回值或副作用

The last step can check either the new state of the object being tested, or the results of its work in the overall environment of the system.

最后一步可以检查被测试对象的新状态,或者它在系统整体环境中的工作结果。

Here is a small example:

这是一个小例子:

class Calculator {
    private int left, right, result;
    public void setLeft(int val) { left = val; }
    public void setRight(int val) { right = val; }
    public int getResult() { return result; }
    public void add() { result = left + right; }
    public void multiply() { result = left * right; }
}

You could test its multiply()method like this:

你可以multiply()像这样测试它的方法:

@Test
public void testMultiply() {
    // Set the object being tested to the initial state 
    Calculator tester = new Calculator();
    tester.setLeft(5);
    tester.setRight(10);
    // Call the method
    tester.multiply();
    // Check the new state
    assertEquals("10 x 5 must be 50", 50, tester.getResult());
}

This sample tests side effects of the method. Testing a method with a return value and no side effects would combine the call of the method with its check on the assertion line.

此示例测试该方法的副作用。测试具有返回值且没有副作用的方法会将方法的调用与其在断言行上的检查结合起来。

回答by Ryan F

In this case you're trying to test the side-effects of the call, rather than the output (e.g. return value) of the call itself. You can test this in a number of different ways:

在这种情况下,您正在尝试测试调用的副作用,而不是调用本身的输出(例如返回值)。您可以通过多种不同的方式对此进行测试:

  1. If the side-effects are visible, test that their results match your expectations
  2. If the side-effects are not easily visible because they call into external interfaces, use a mocking frameworksuch as PowerMockto replace the external interface with mock objects so that you can observe and test against the calls to the external interface
  3. Redesign your interface to be more testable. :)
  1. 如果副作用可见,请测试其结果是否符合您的预期
  2. 如果副作用因为调用外部接口而不易被看到,可以使用模拟框架(例如PowerMock)将外部接口替换为模拟对象,以便您可以观察和测试对外部接口的调用
  3. 重新设计您的界面,使其更具可测试性。:)

In your specific case I'd add an accessor for al and check its contents match expected after the call to AMethod().

在您的特定情况下,我会为 al 添加一个访问器,并在调用 AMethod() 后检查其内容是否匹配。

In general for unit testing, you don't want to connect to external URLs, databases, etc because it's slow and expensive. You want to replace those calls with data-providers which can simulate them instead.

一般来说,对于单元测试,您不想连接到外部 URL、数据库等,因为它既慢又昂贵。您想用可以模拟它们的数据提供程序替换这些调用。

回答by chiastic-security

Yes, certainly you can test a method with no parameters.

是的,您当然可以测试没有参数的方法。

It may be that an example is the best way of seeing this. Think about the ArrayList.clear()method. What it does is to empty the ArrayList, so that its size is then zero. You'd test this method by:

一个例子可能是看待这一点的最佳方式。想想ArrayList.clear()方法。它所做的是清空ArrayList,使其大小为零。您可以通过以下方式测试此方法:

  1. Creating an ArrayList arr;
  2. Adding some things to it;
  3. Invoking arr.clear();
  4. Checking assertEquals(arr.size(),0).
  1. 创建一个ArrayList arr;
  2. 添加一些东西;
  3. 调用arr.clear()
  4. 检查assertEquals(arr.size(),0)

In other words, you're setting things up so that your ArrayListhas a state you don't want (size()>0); then you're invoking the method that should have a predictable effect on the state (setting size()to 0); then checking that the effect is as you expected.

换句话说,您正在设置一些东西,以便您ArrayList拥有不想要的状态 ( size()>0);那么你调用的方法应该对状态产生可预测的影响(设置size()0);然后检查效果是否符合您的预期。

回答by Moni

Yes you can test these method if you are using these method in other methods as a side effects. As for example-

是的,如果您在其他方法中使用这些方法作为副作用,您可以测试这些方法。比如——

public class test{
public int i=0;
public void incrementIngeterValue()
{
  //doing something and 
   i++;
}

public boolean incremented()
{
  //some logic to check what is current i value
  int x = incrementIngeterValue();

  if(x==i)
       return false; // value not incremented
  else
       return true // in this case you can make sure this value is incremented by 1.


}

So you can test incremented method for checking i value is incremented by 1 or not, with this method you are internally test your incrementIngeterValue() without parameter and return value.

因此,您可以测试用于检查 i 值是否增加 1 的增量方法,使用此方法您可以在内部测试您的 incrementIngeterValue() 没有参数和返回值。

In your case you a1 is effected in both methods. somewhere you are using this a1.

在您的情况下,您 a1 在这两种方法中都受到影响。你在某个地方使用这个 a1。