13

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 );
  }
}
Jon O
  • 6,267
  • 1
  • 41
  • 54
  • Here you would find answer: http://stackoverflow.com/questions/29611893/mockito-notamockexception – piotrek1543 Dec 18 '15 at 08:19
  • You'll have to be more detailed. The point of the question is that it *is* a mock, as clearly indicated in the class name in the stack trace: `ServiceCallbackBase$$EnhancerByMockitoWithCGLIB$$9acf906b` – Jon O Dec 18 '15 at 16:18

2 Answers2

17

I cannot build your example, but I managed to write this minitest that produces a very similar problem:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@PrepareOnlyThisForTest({ServiceCallbackBase.class, Dummy.class})
public class MainActivityTest {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Test
    public void test1() throws Exception {
        try {
            //This Mockito.withSettings() thing is important to make the test fail!
            ServiceCallbackBase callback = PowerMockito.mock( ServiceCallbackBase.class, Mockito.withSettings());

            callback.dispatchMessage(null);
            Mockito.verify(callback).dispatchMessage(null);

        } catch (Exception e){
            e.printStackTrace();
            Assert.fail();
        }
    }
}

(Note the Mockito.withSettings(), I don't know why but that makes the test to fail)

Prints:

org.mockito.exceptions.misusing.NotAMockException: 
    Argument passed to verify() is of type ServiceCallbackBase$$EnhancerByMockitoWithCGLIB$$62776c54 and is not a mock!
    Make sure you place the parenthesis correctly!
    ......

Well, this absolutely looks as a classloading issue, mockito is comparing ServiceCallbackBase$$EnhancerByMockitoWithCGLIB$$etc.. loaded by Powermock with the same loaded by Robolectric (obviously returning false in that comparision)

Then, I managed to make the test work simply adding "org.powermock.*"to the line @PowerMockIgnore... this is:

@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "org.powermock.*"})

This simple change made my test to work, and I really hope that make yours too.

fonkap
  • 2,382
  • 1
  • 12
  • 28
  • I cannot give you enough upvotes for this. I dunno why, but that fixed it. Thank you! – Jon O Feb 22 '16 at 21:18
  • 1
    I had the same problem and this fixed if for the time being. Make sure however that you add `"org.powermock.*"` to the `PowerMockIgnore` annotation on **each** test class – andy9775 Jun 23 '16 at 00:48
2

I ran into this problem too. There is actually an issue on the PowerMock project: https://github.com/jayway/powermock/issues/593

But no comments from any of the powermock developers.

Dustin
  • 863
  • 8
  • 10