Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 478
Jarek Avatar asked Jun 17 '11 08:06

Jarek


1 Answers

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();
like image 130
hoipolloi Avatar answered Oct 30 '22 18:10

hoipolloi