如何验证Mockito中的通知

时间:2013-10-18 07:25:33

标签: java junit mockito

我们如何测试一些发送出去的通知?

例如,在下面的代码中,上下文是外部依赖项。在编写Junit时,我嘲笑它。有没有办法验证执行了哪个“sendNotification”调用?

Class SomeClass{
 JsonRpcContext context;
  public void someMethod(String arg1,String arg2) throws Exception {
    if(someConditionIsMet){
            //Do some stuff
            context.sendNotification("agentservice", "agentconnected", "Agent session started");
     }else{
         //Do Some Stuff
        context.sendNotification("agentservice", "agentnotconnected", "Error occurred on server side for agent session start");
     }
 }

}

1 个答案:

答案 0 :(得分:2)

如果您创建context,请执行以下操作:

context = mock(JsonRpcContext.class);

那么这应该足够了:

verify(context).sendNotification("agentservice", "agentconnected", "Agent session started");

verify(context).sendNotification("agentservice", "agentnotconnected", "Error occurred on server side for agent session start");