8

I am writing a unit test to check the email validation logic. The logic is throwing null pointer exception when you run the test. But it works fine with emulator. Can someone help me solving this?

public static String validate(String email, String password) {
        if (email == null || email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            return "Enter valid email address";
        }
        if (password.isEmpty() || password.length() < 4) {
            return "Password should be between 4 and 10 alphanumeric characters";
        }
        return null;
}

Below is my unit test.

@Test
public void validateShouldReturnMessageIfYouPassAnInvalidEmail() throws Exception {
    String validateMessage = LoginService.validate("abcdef", "password");
    assertEquals("Enter valid email address", validateMessage);
}

The error I am getting is,

java.lang.NullPointerException
at com.tryout.hemanth.expensetracker.service.LoginService.validate(LoginService.java:11)
Hemanth S R
  • 1,115
  • 2
  • 16
  • 27

4 Answers4

24

Use PatternsCompat instead of Patterns

Anjula
  • 1,690
  • 23
  • 29
2

android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() exists since API level 8, May be your emulator is prior than this.

So I suggest Adding custom EMAIL_ADDRESS_PATTERN string as suggested in this answer and check it like

if (email == null || email.isEmpty() || !EMAIL_ADDRESS_PATTERN.matcher(email).matches()){
     return "Enter valid email address";
}
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • But the same code works fine with emulator. I don't know why android is null when i run it through Junit. – Hemanth S R May 18 '16 at 15:08
  • Are you sure you are using JUNIT latest like 4. Or your device's API level is compatible ? [see](https://developer.android.com/topic/libraries/testing-support-library/index.html#ajur-junit) the requirements and rules of testing. – Shree Krishna May 18 '16 at 15:14
  • This issue is not because of emulator but the android sdk version. In my case Patterns.EMAIL_ADDRESS works as expected below version 28 but it suddenly returns null on version 29. – Amey Bhandarkar Aug 13 '20 at 09:22
1

In case if Patterns.EMAIL_ADDRESS doesn't match with your requirements,

create your own regex pattern for better use.

Please refer to this EMAIL ADDRESS regex for instance.

Necromancer
  • 869
  • 2
  • 9
  • 25
  • Because the regex is complex does't mean it is not right. What if you have to validate an email your simpler regex does not cover? – Kaizie Apr 09 '18 at 14:16
  • @Kaizie Yes you are right. I mean you can customize you validation as you want. Complex or simple is based on you business logic – Necromancer May 19 '18 at 06:17
0

try this, this is working for me :

fun isValidEmail(email:String?):Boolean {
return when{
    email.isNullOrEmpty()-> false
    else-> PatternsCompat.EMAIL_ADDRESS.matcher(email).matches();
}

}

Test cases:

 @Test
fun validate_email_invalid() {
    val email = "a"
    Assert.assertEquals(false, isValidEmail(email))
}
Yogendra
  • 4,817
  • 1
  • 28
  • 21