Search code examples
unit-testinggwtmockitogwtmockito

Mockito Verify method not giving consistent results


I'm learning GwtMockito but having trouble getting consistent verify() method results in one of my tests.

I'm trying to test the correct GwtEvents are being fired by my application. So I've mocked the Event Bus like this in my @Before method:

eventBus = mock(HandlerManager.class);

This test passes as expected:

// Passes as expected
verify(eventBus).fireEvent(any(ErrorOccurredEvent.class));

I wanted to force the test to fail just to know it was running correctly. So I changed it to this and it still passes:

// Expected this to fail, but it passes
verify(eventBus).fireEvent(any(ErrorOccurredEvent.class));
verifyZeroInteractions(eventBus).fireEvent(any(ErrorOccurredEvent.class));

This seems contradictory to me. So I removed the first test:

// Fails as expected
verifyZeroInteractions(eventBus).fireEvent(any(ErrorOccurredEvent.class));

Finally I added an unrelated event that should cause it to fail

// Expected to fail, but passes
verify(eventBus).fireEvent(any(ErrorOccurredEvent.class));
verify(eventBus).fireEvent(any(ModelCreatedEvent.class));  // This event is not used at all by the class that I'm testing. It's not possible for it to be fired.

I'm not finding any documentation that explains what's going on. Both ErrorOccurredEvent and ModelCreatedEvent extend GwtEvent, and have been verified in manual testing. Am I testing my EventBus incorrectly? If so, what is a better way to go about it?

Update

I've done some additional experimenting. It appears to be an issue I'm having with the Mockito matcher. When I get the test to fail the exception reports the method signature as eventBus.fireEvent(<any>) so it doesn't appear to be taking into account the different classes I'm passing into the any method. Not sure what to do about this yet, but including it here for anyone else researching this problem.


Solution

  • The method you're looking for is isA, instead of any.