本文介绍了如何模拟/测试返回void的方法,可能在Mockito中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我找不到一个优雅的解决方案。

I came across a problem and I can't find an elegant solution.

所以问题在于模拟Selenium网络驱动程序,我不知道如何我应该测试/模拟void方法。

So the problem is with a mock of Selenium web driver, and I dont know how should I test/mock void methods.

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

所以我要问的是,如何要正确测试这样一个方法,一个测试将是抛出异常,但是如何正确地测试我委托的那个void方法?

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:

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

// following throws RuntimeException:
mockedList.clear();

这篇关于如何模拟/测试返回void的方法,可能在Mockito中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:17