2013-08-29 49 views
0

我目前在這個春天mvc應用程序中使用mockito 1.8.4。這是我正在測試的類/方法的代碼。Mockito嘲笑不適用於此方法。難道我做錯了什麼?

public class CompleteTaskController implements IController { 
    public static Logger log = Logger.getLogger(CompleteTaskController.class); 

@Override 
public void handle() { 
    GUIFactory gf = new GUIFactory(); 
    IDatabasePullListOfUsers pull = new OraclePullListOfUsers(); 
    IDatabaseUserManagement manage = OracleUserManagement.getInstance(); 

    gf.makeGUI("completeTask", pull.pullAssignedRequests(GUIFactory.userLoggedIn)); 
    manage.completeTask(gf.getRequestID(), GUIFactory.userLoggedIn); 
    gf.makeCustomGUI("Task has been completed"); 
    log.fatal(GUIFactory.userLoggedIn + " has completed the task of request id " + gf.getRequestID()); 
    gf.makeGUI("adminpanel"); 

} 

} 

到目前爲止,所有的測試都通過了但最後一個 - 我似乎無法理解爲什麼。下面是我的測試代碼:

public class CompleteTaskControllerTest { 

    @Test 
    public void testHandleCallsMakeGUIAndPassesItPullAssignedRequestsAndAString(){ 
     CompleteTaskController mockCtc = mock(CompleteTaskController.class); 
     GUIFactory mockGf = mock(GUIFactory.class); 
     IDatabasePullListOfUsers mockPull = mock(OraclePullListOfUsers.class); 

     mockCtc.handle(); 

     verify(mockGf).makeGUI("test", mockPull.pullAssignedRequests("test")); 
    } 

    @Test 
    public void testHandleCallsCompleteTaskAndPassesItGetRequestIDAndAString(){ 
     CompleteTaskController mockCtc = mock(CompleteTaskController.class); 
     IDatabaseUserManagement mockManage = mock(OracleUserManagement.class); 
     GUIFactory mockGf = mock(GUIFactory.class); 

     mockCtc.handle(); 
     when(mockGf.getRequestID()).thenReturn(1); 

     verify(mockManage).completeTask(mockGf.getRequestID(),"Test"); 
    } 

    @Test 
    public void testHandleCallsMakeCustomGUIAndPassesItAString(){ 
     CompleteTaskController mockCtc = mock(CompleteTaskController.class); 
     GUIFactory mockGf = mock(GUIFactory.class); 

     mockCtc.handle(); 

     verify(mockGf).makeCustomGUI("test"); 
    } 
} 

對不起投擲所有這些代碼在你 - 我有問題的唯一測試是第三個測試 - 這是試圖嘲笑gf.makeCustomGUI(「任務有已完成「)方法!

+0

嗯,你如何獲得mockGf注入mockCtc?我只知道通過註釋或手動方式...只是一個想法... – treeno

回答

1

的發言

verify(mockGf).makeCustomGUI("test"); 

你想驗證「makeCustomerGUI」是否已被調用參數「測試」。如果這是你的要求,你可以做如下:

GUIFactory mockGf = mock(GUIFactory.class); 
mockGf.makeCustomerGUI("test"); 
verify(mockGf).makeCustomGUI("test"); 

一旦你的模擬對象被創建,將的Mockito記得它所有調用。所以你可以有選擇地驗證調用。

+0

感謝您的答覆。然而,我試圖檢查handle()方法是否調用mockGf.makeCustomGUI(「anystringhere」) – DeaIss

+1

「GUIFactory gf」是方法的本地方,它不是「CompleteTaskController」對象的屬性,所以不能測試「句柄」方法時,在「GUIFactory」上調用測試方法。 –

+0

爲什麼測試1和2成功通過?我認爲它調用了處理方法,然後驗證檢查是否調用了方法! – DeaIss