How can one test a function that does not return a value, but uses given arguments to construct some values which are being sent to a sequential function?
For example
public void handleSomeEvent(String str1, String str2){
MyObject obj1 = new MyObject();
obj1.setParameterA(str1);
obj2.setParameterB(str2);
EventHandler.getInstance().notify(obj1)
}
In the above example i would like to verify EventHandler.notify
was called with an object containing str1
as parameterA
and str2
as parameter2
. Can this be done using some common mocking frameowrk (i.e mockito)?
You can do following
(side note this quick code is to demonstrate the functionality not best practises in coding)
In cases when need to verify called parrams use
ArgumentCaptor<T>
by MockitoSee the reference: https://static.javadoc.io/org.mockito/mockito-core/2.6.9/org/mockito/ArgumentCaptor.html
Unittest verify public observable behavior where "public" means through the units API and "behavior" meand return values and/or communication with dependencies.
Your code has a hidden dependecy intoduced by the static access to the
EventHandler
object. I assume that yourEventHandler
class incorporates the Java singelton pattern. This is STUPID code most of us have been started with.You should not surrender to this bad design by using
PowerMock.The better way would be to pass an
EventHandler
object as constructor parameter to your code under test like this:And then make your
EventHandler
a normal class that you can inherit from. (at least remofe thefinal
key word from the class declaration).Then you could use plain Mockito to replace the
EventHandler
object with a test double and verify your codes comunication with that test double: