Assume following class with private and public static methods and we want to write junit test which access those methods. We are mocking these static methods.
Mock Private Static Method
For mocking private static method we need to use
Mock Public Static Method
When mocking public static method we could use either of following:
OR
Here is the implementation
BE CAREFUL NOT USE FOLLOWING WITH PUBLIC STATIC METHOD
This will give
ERROR
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
No comments:
Post a Comment