2013-10-18 46 views
0

我們如何測試某些通知發送出課程?如何驗證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"); 
    } 
} 

}

回答

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"); 
+0

哇,太容易了。非常感謝。將接受答案...有10分鐘的帽子:) – cpandey05

+0

不客氣:) –