简体   繁体   中英

Mockito mocking won't work for this method. Am I doing something wrong?

I'm current using mockito 1.8.4 in this spring mvc application. Here is the code for the class/method I am trying to test.

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

}

}

so far, all the tests pass BUT the last one - which I just can't seem to understand why. Here is the code for my tests:

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");
    }
}

Sorry for throwing all this code at you - the only test I am having issues with is the third test - which is trying to mock the gf.makeCustomGUI("Task has been completed") method!

With the statement

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

are you trying to verify whether the "makeCustomerGUI" has been called with argument "test". And if that is your requirement, you can do that as below:

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

Once your mock object is created, Mockito will remember all invocations on it. So you can selectively verify the invocations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM