I am testing a function of my Signup class, however every time I run it a NullPointerException occurs inside the first if statement it enters at:
pass.show()
I suspect it is has something to do with not testing the app with an emulator. How can I prevent errors of creating a Toast when I am testing this?
Function in Signup Class:
public boolean checkInput(String username, String email, String password, String confirmpassword) {
if (!email.contains("@") || !email.contains(".com")) {
Toast pass = Toast.makeText(Signup.this, "Email must be valid!", Toast.LENGTH_SHORT);
pass.show();
return false;
}
if (!password.equals(confirmpassword)) {
Toast pass = Toast.makeText(Signup.this, "Passwords don't match!", Toast.LENGTH_SHORT);
pass.show();
return false;
}
if (!(password.length() >= 6)) {
Toast pass = Toast.makeText(Signup.this, "Password must be at least 6 characters", Toast.LENGTH_SHORT);
pass.show();
return false;
}
else {
return true;
}
}
Test method:
@Test
public void testInvalidEmail() {
String testUser = "testUser";
String testEmail = "testEmail.com"; //invalid
String testPass = "testPass";
String testConfirmPass = "testPass";
assertFalse(signupClass.checkInput(testUser,testEmail,testPass,testConfirmPass));
}