0

我们如何测试一些课外发送的通知?

例如,在下面的代码中,上下文是一个外部依赖项。在写 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");
     }
 }

}

4

1 回答 1

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");
于 2013-10-18T07:27:57.047 回答