I'm starting to work with unit tests in android I have trying to test a method that uses:
android.util.Patterns.EMAIL_ADDRESS.matcher(validEmail).matches()
it should return true as I added this in my build.gradle
testOptions {
unitTests {
returnDefaultValues = true
}
}
Still the test throws a NullPointerException.
I have two questions: 1- How do I fix this? 2- Should I reconsider my design and remove the android dependency to a mock object.
// @ LoginPresenterTest
@Test
public void clickOnLogin_loginSuccess(){
loginPresenter.login(validEmail, validPassword);
verify(loginView).setLoginButton(false);
}
// LoginPresenter
public void login(String email, String password) {
loginView.setLoginButton(false);
if(!isValid(email, password)){
loginView.setLoginButton(true);
return;
}
}
// Validation
public static boolean isEmailValid(String email){
return !(email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches());
}
When android.utills is commented their will be no exception.
static methods
are not mocked. You need to use some mocking framework like mockito
or powermock
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With