简体   繁体   English

如何模拟/测试返回void的方法,可能在Mockito中

[英]How to mock/test method that returns void, possibly in Mockito

I came across a problem and I can't find an elegant solution. 我遇到了一个问题,我找不到一个优雅的解决方案。

So the problem is with a mock of Selenium web driver, and I dont know how should I test/mock void methods. 所以问题是模拟Selenium web驱动程序,我不知道我应该如何测试/模拟void方法。

public void clickAndWait(String locator) {
    if(isElementPresent(locator) == false) throw some exception;
    selenium.clickAndWait(); //a problematic delegating call to selenium
}

So what I am asking is, how to properly test such a method, one test would be for exception being thrown, but how properly make test of that void method I delegate to? 所以我要问的是,如何正确测试这样的方法,一个测试将被抛出异常,但是如何正确地测试我委托给的void方法?

The following code sample from this Mockito documentation illustrates how to mock a void method: Mockito文档中的以下代码示例说明了如何模拟void方法:

doThrow(new RuntimeException()).when(mockedList).clear();

// following throws RuntimeException:
mockedList.clear();
doAnswer(new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {

            return null;
        }
    }).when(mock).method((SomeClass) anyObject());

The previous answers have been stressing on doing something (throwing an exception possibly) at every call. 之前的答案一直强调在每次通话时做某事(可能抛出异常)。 This way when you do something like : 这样你做的事情如下:

doThrow(new RuntimeException()).when(mockedList).clear();

and then call the stubbed service (or logic) like : 然后调用存根服务(或逻辑),如:

mockedList.clear();

it will generate an exception. 它会产生一个例外。 What if you want to test for a proper functioning of method maybe writing positive test case. 如果你想测试方法的正确功能怎么办可能会写出积极的测试用例。 Mocking a void returning method for such case could be done by : 对这种情况进行模拟返回的方法可以通过以下方式完成:

doNothing().when(mockedList).clear();

which means that since you stubbed the clear() method for mockedList mock, you can be sure that this method is not going to effect the logic of the unit and still you can check the rest of the flow without generating an exception. 这意味着,因为你为mockedList mock存根了clear()方法,你可以确定这个方法不会影响单元的逻辑,你仍然可以在不产生异常的情况下检查流的其余部分。

You can also use: 您还可以使用:

  • The method Mockito.verify(mock/spy) to check how many times the method has been called. 方法Mockito.verify(mock / spy)检查方法被调用了多少次。
  • Or use the argument captor to see/check some parameters passed to the void method. 或者使用参数captor查看/检查传递给void方法的一些参数。

You can trow an exception on your method call, here is a small example how to do it: 您可以在方法调用上抛出异常,这是一个小例子如何做到:

doThrow(new RuntimeException()).when(mockedList).clear();

then you call mockedList.clear(); 然后你调用mockedList.clear(); mocked method will throw an exception. mocked方法会抛出异常。

Or you can count how many times your method was called, here is a small example how to do it: 或者你可以计算你的方法调用次数,这里有一个小例子如何做:

verify(mockedList, times(1)).clear(); 

In Java 8 this can be made a little cleaner 在Java 8中,这可以变得更加清晰

doAnswer((i) -> {
  // Do stuff with i.getArguments() here
  return null;
}).when(*mock*).*method*(*methodArguments*);

The return null; return null; is important and without it the compile will fail with some fairly obscure errors as it won't be able to find a suitable override for doAnswer . 很重要,如果没有它,编译将失败并出现一些相当模糊的错误,因为它无法为doAnswer找到合适的覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM