Saturday 17 August 2019

java - How to verify that a specific method was not called using Mockito?

First of all: you should always import mockito static, this way the code will be much more readable (and intuitive):



import static org.mockito.Mockito.*;


There are actually many ways to achieve this, however it's (arguably) cleaner to use the



verify(yourMock, times(0)).someMethod();



method all over your tests, when on other Tests you use it to assert a certain amount of executions like this:



verify(yourMock, times(5)).someMethod();


Alternatives are:



verify(yourMock, never()).someMethod();



Alternatively - when you really want to make sure a certain mocked Object is actually NOT called at all - you can use:



verifyZeroInteracions(yourMock)

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...