Search code examples
javajunitmockitomatcherhamcrest

InvalidUseOfMatchersException


I'm busy with writing a Junit test with Mockito.

Now I want to verify something like this:

verify(event).fire(
   new DefaultMonitoringEventImpl(
      any(Class.class), any(MonitorEventType.class), MonitorEventLevel.ALL, anyString()
   )
  );

I only care about the third parameter. when I try this I get a: InvalidUseOfMatchersException.

Whatever i try it wont fix this issue. Related topics won's give a satisfied solution.

-Bgvv1983


Solution

  • Use ArgumentCaptor:

    ArgumentCaptor<DefaultMonitoringEventImpl> captor = ArgumentCaptor.forClass(DefaultMonitoringEventImpl.class);
    Mockito.verify(event).fire(captor.capture());
    DefaultMonitoringEventImpl actual = captor.getValue();
    Assert.assertEquals(MonitorEventLevel.ALL, actual.getMonitorEventLevel());