Jarek Jarek - 2 years ago 173
Java Question

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.

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?

Answer Source

The following code sample from this Mockito documentation illustrates how to mock a void method:

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

// following throws RuntimeException:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download