Hi,
I finally managed to factor out the problem with the code:
I have a static final void (phew!) method in LoggingApi that makes it trickier. I have tried 2 ways in my testcase ( method annotated with @Test) to get around this but I have not managed.
I am extending PowerMockTestCase and @PrepareForTest(LoggingApi.class)
These are the alternatives:
1. PowerMockito.doNothing().when(LoggingApi.class, "setTestEnd");
Message:
java.lang.NullPointerException
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.addAnswersForStubbing(PowerMockitoStubberImpl.java:67)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:42)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:105)
at example.MyTestCase.myTestCase(MyTestCase.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2. PowerMockito.mockStatic(LoggingApi.class);
final LoggingApi logginApi = mock(LoggingApi.class);
PowerMockito.doNothing().when(logginApi).setTestEnd();
Message:
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!
at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:260)
at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:123)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:62)
at <company package>LoggingApi.setTestEnd(tLoggingApi.java)
at example.MyTestCase.myTestCase(MyTestCase.java:47)
I guess I am doing something syntactically wrong!
Any hints?
//mike