Bit of a complicated setup. Robolectric, PowerMockito rule-based config.
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
// Using "PrepareOnlyThis" prevents powermock from trying to instrument the whole hierarchy,
// part of which we've ignored (android.os.* in this case)
@PrepareOnlyThisForTest({ServiceCallbackBase.class}) // this class extends Handler,
// so we need PrepareOnlyThis. It also has some final methods we need to verify()
public class ServiceBaseTests {
private class Foo {
// nothing
}
@Rule
public PowerMockRule rule = new PowerMockRule();
private ServiceCallbackBase<Object, Foo> setupCallback( boolean hasValidContext, boolean allContextsCanceled ) {
ServiceCallbackBase<Object, Foo> callback = PowerMockito.mock( ServiceCallbackBase.class );
// EDIT: I have converted these to PowerMockito.doReturn()s to no avail.
PowerMockito.when( callback.hasValidContext() ).thenReturn( hasValidContext );
PowerMockito.when( callback.allContextsAreCanceled( any( Message.class ) ) ).thenReturn( allContextsCanceled );
PowerMockito.doNothing().when( callback ).preSendMessage( any( Message.class ) );
return callback;
}
Should be pretty routine. But whenever I try to call verify
on one of these "callback" mock instances, for instance:
private void test_notifyCallback( boolean isFromCache ) {
ServiceCallbackBase<Object, Foo> callback = setupCallback( true, false );
uut.addHandler( TestEnum.FOO, callback );
uut.addHandler( TestEnum.BAR, PowerMockito.mock( ServiceCallbackBase.class ) );
uut.addHandler( TestEnum.BAZ, PowerMockito.mock( ServiceCallbackBase.class ) );
Response<Foo> foo = new Response<>( new Foo(), new ResponseStatus( 0, "Error" ) );
uut.handleCallBack( TestEnum.FOO, foo, isFromCache );
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass( Message.class );
// this line throws the error.
verify( callback ).preSendMessage( captor.capture() );
assertThat( captor.getValue().what ).isEqualTo( TestEnum.FOO.ordinal() );
assertThat( captor.getValue().obj ).isEqualTo( foo );
assertThat( captor.getValue().arg1 ).isEqualTo( isFromCache ? 1 : 0 );
}
I get an error like so:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type ServiceCallbackBase$$EnhancerByMockitoWithCGLIB$$9acf906b and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
It's clearly and obviously been "enhanced" by mockito, and PowerMock doesn't have a verify() method to use instead of Mockito.verify()
... what gives?
EDIT: this is in some ways more and in some ways less confusing.
I'm in the process of building another test class to test ServiceCallbackBase itself. If I remove the tests from that class, these tests pass. The following snippet in a different class causes the tests above to fail.
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class ServiceCallbackBaseTests {
@Test
public void test_nothing(){
}
private ServiceCallbackBase<Object, String> uutSpy;
@Before
public void setup(){
uutSpy = mock( ServiceCallbackBase.class );
}
}