Frage

I would like to check if a method inside another a method gets call (if another method returns false!)

It sounds a bit complicated, but if I provide my code it should be much easier to understand.

Does anyone know if this is possible?

I want to check if the following method calls RegisterGUI.errorDuplicateUsername() when usermanagement.storeUser(user1) returns false.

public class RegisterController {

    public void regHandle(UserDataObject user1){

        ValidateRegisterInputController validate = new ValidateRegisterInputController();
        validate.validateInputHandle(user1); 

        OracleUserManagement usermanagement = OracleUserManagement.getInstance();
        if (usermanagement.storeUser(user1) == false) { 
            RegisterGUI.errorUsernameDuplicate(); }

    }
}

This is what I have thus far:

/** Step1. Mock UserDataObject and stub OracleUserManagement.storeUser(UserDataObject) to return false.
     *  Step2. Check if RegisterController.regHandle(MockedUserDataObject) calls RegisterGUI.errorDuplicateUsername
     *  Step3. See if Test passes!
     */

    @Test
    public void testRegisterControllerregHandleCallsLoginGUIerrorDuplicateUsernameWhenOracleUserManagementstoreUserReturnsFalse(){
        UserDataObject user1mocked = mock(UserDataObject.class);
        OracleUserManagement usermanagementmocked = mock(OracleUserManagement.class);

        when(usermanagementmocked.storeUser(user1mocked)).thenReturn(false);

            /** This is where I'm a bit confused. How can I test that when I run this
            * method, it will call RegisterGUI.errorDuplicateUsername()?
            */
        RegisterController regcontroller = new RegisterController();
        regcontroller.regHandle(user1mocked);

        verify(RegisterGUI.errorUsernameDuplicate())


    }
}

However, it says I cannot use the verify method

War es hilfreich?

Lösung

I assume the RegisterGUI is a class name, hence the method is static. Obviously in Java it is impossible to override static methods, so you cannot do it with mockito. I would recommend refactor code and don't use static methods.

However, if you are dealing with legacy code you could use Powermock which manipulates bytecode and overrides statics.

Another approach to work with legacy code is to extract the static method call into a new method. So your controller will be:

public class RegisterController {

    public void regHandle(UserDataObject user1){

        ValidateRegisterInputController validate = new ValidateRegisterInputController();
        validate.validateInputHandle(user1); 

        OracleUserManagement usermanagement = OracleUserManagement.getInstance();
        if (usermanagement.storeUser(user1) == false) { 
            errorUsernameDuplicate(); }

    }

    void errorUsernameDuplicate() {
        RegisterGUI.errorUsernameDuplicate();
    }
}

Now you could use mockito spy to verify your controller:

verify(regcontroller).errorUsernameDuplicate()

Obviously the method itself becomes untested, however it is trivial and it is not a problem.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top