-1
나는이 다음 시험 방법 :Mockito 모의 작동하지 않습니다 제대로
@Override
public Boolean register(String email, String name, String password, String password2) {
if (password.equals(password2)) {
try {
String pwd = hashPassword(password);
User user = new User(email, name, pwd);
AuthStorage authStorage = new AuthStorageImpl();
authStorage.registerNewUser(user);
return true;
} catch (NoSuchAlgorithmException | AuthStorageException e) {
return false;
}
}
// If passwords don't match
return false;
}
아마, 그 다음 예외를 thow해야하고 registerNewUser
전화 : 다음의 클래스 메소드를 테스트
@RunWith(MockitoJUnitRunner.class)
public class AccountManagerTest {
@InjectMocks
private AccountManager accountManager = new AccountManagerImpl(null);
@Mock
private AuthStorage authStorage;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
/* REGISTER TESTS */
@Test
public void test_whenRegister_withAlreadyExistingEmail_thenDoNotRegister() throws AuthStorageException {
String email = "[email protected]";
String name = "Foo";
String password = "123456";
String password2 = "123456";
doThrow(new AuthStorageException("Email already in use")).when(authStorage).registerNewUser(Matchers.any());
assertFalse(accountManager.register(email, name, password, password2));
}
}
메서드는 false
을 반환하지만 디버깅 할 때 예외가 throw되지 않고 프로그램이 true
을 반환하는 것을 볼 수 있습니다. 내가 도대체 뭘 잘못하고있는 겁니까? 당신이 모의 객체가 삽입 된 객체 인스턴스화 안 모든
try 'this = new AccountManagerImpl (null);' – vikingsteve
이미 시도해 보았습니다. 이것은 Mockito가 인터페이스를 초기화 할 수 없기 때문에 수행 할 수 없습니다. @InjectMocks private AccountManagerImpl accountManager 동일한 오류가 발생합니다 – JosepRivaille
질문에 대한 답변은 [이 페이지] (https : // github)에 나와 있습니다. .com/mockito/mockito/wiki/Mocking-Object-Creation) –