【问题标题】:InvalidUseOfMatchersExceptionInvalidUseOfMatchersException
【发布时间】:2016-10-21 09:51:30
【问题描述】:

我正忙于用 Mockito 编写一个 Junit 测试。

现在我想验证这样的事情:

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

我只关心第三个参数。 当我尝试这个时,我得到一个:InvalidUseOfMatchersException。

无论我尝试什么都不会解决这个问题。 相关主题将提供满意的解决方案。

-Bgvv1983

【问题讨论】:

    标签: java junit mockito matcher hamcrest


    【解决方案1】:

    使用ArgumentCaptor:

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

    【讨论】:

    • 谢谢,愚蠢的我没想到。不过,对您的解决方案有一点评论。 Mockito.verify(event).fire(captor);必须是:Mockito.verify(event).fire(captor.capture());
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多