我想检查另一个方法中的方法是否被调用(如果另一个方法返回false!)
这听起来有点复杂,但如果我提供我的代码,它应该更容易理解。
有谁知道这是否可能?
当 usermanagement.storeUser(user1) 返回 false 时,我想检查以下方法是否调用 RegisterGUI.errorDuplicateUsername()。
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(); }
}
}
这是我迄今为止所拥有的:
/** 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())
}
}
但是,它说我不能使用验证方法