I have a problem like this
public class ErrorLog {
public static void increment(String exception) {
// do something
}
}
import ErrorLog;
public class Calculator {
public int division(int a, int b) {
if (b == 0) {
ErrorLog.increment("Divide by Zero");
} else {
return a / b;
}
}
}
I want to verify the number of calls to divide, but when I have divide by zero situation, somehow verify that the static method ErrorLog.increment is called (with the exact string as the parameter).
With Mockito spy, I can do something like this to ensure calls to the divide method.
Calculator c = new Calculator();
Calculator calcSpy = spy(c);
c.division(6, 3);
c.division(1, 0);
verify(calcSpy, times(2)).division(anyInt(), anyInt());
I also keen to verify something like this
verify(ErrorLog, times(1)).increment("Divide by Zero");
I explored PowerMockito verifyStatic constructs and mockStatic, checked this one too mockito verify method call inside method but in my case the method that is called inside the object is static.
You are right verifyStatic
doesn't have any ways to check the method being called.
But there is a workaround. See if this works for you.
The way to mock a static method is to use
PowerMockito.mockStatic(ErrorLog.class)
But you can also use another variant
PowerMockito.mockStatic(ErrorLog.class, invocationOnMock -> {
assertEquals("something", invocationOnMock.getArgumentAt(0, String.class));
return null;
});
ErrorLog.increment("something");
If you do not mock ErrorLog class, you cannot verify