In my android app, I take the input from the edit text when the login button is clicked and pass it to my presenter. The presenter then validates it using the LoginValidator
utility class, depending on the result it either continues with the normal login flow or sends a message to the activity notifying that the email or/and password is/are invalid.
In the test class for the LoginValidator
I do the following:
public class LoginValidatorTest {
private static final String INVALID_EMAIL = "ewksd";
private static final String VALID_EMAIL = "tom.finet@gmail.com";
private static final String INVALID_PASSWORD = "sjadsaA";
private static final String VALID_PASSWORD = "Asdfgh1d";
@Test
public void shouldValidateInvalidEmail() {
Assert.assertEquals(LoginValidator.validateEmail(INVALID_EMAIL), false);
}
@Test
public void shouldValidateValidEmail() {
Assert.assertEquals(LoginValidator.validateEmail(VALID_EMAIL), true);
}
@Test
public void shouldValidateInvalidPassword() {
Assert.assertEquals(LoginValidator.validatePassword(INVALID_PASSWORD), false);
}
@Test
public void shouldValidateValidPassword() {
Assert.assertEquals(LoginValidator.validatePassword(VALID_PASSWORD), true);
}
}
Here is the LoginValidator
class which the test calls:
public class LoginValidator {
/**
* @param email the email to be validated
* @return true if the email is valid
*/
public static boolean validateEmail(String email) {
return email != null && Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
/**
*
* @param password the password to be validated
* @return true if the password is valid
*/
public static boolean validatePassword(String password) {
if (password == null)
return false;
else if (password.length() < 6)
return false;
else if (!password.contains("^[0-9]"))
return false;
else if (!password.contains("[A-Z]+"))
return false;
return true;
}
}
When the tests are running these are the results:
How do I fix my code to make all the tests pass?
UPDATE:
Here is what the regex for the password validation looks like:
From further analysis, I have concluded that the code Patterns.EMAIL_ADDRESS
is null and hence when calling .matcher(email)
on it causes a NullPointerException. Why the hell is Patterns.EMAIL_ADDRESS
returning null?