Android.util.Patterns.EMAIL returns null during unit test

I have a code that has android.util.Patterns.EMAIL_ADDRESS in the validator. It works fine when it works with the device, but when I run this code in unit test, it returns null. In addition, I tried to copy and paste the internal code into the templates in the following example.

validateEmail1 works // whyyy ???

validateEmail2 returns null

private static final Pattern EMAIL = Pattern.compile( "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\." + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+" ); public boolean validateEmail1(String email) { return EMAIL.matcher(email).matches(); } public boolean validateEmail2(String email) { return Patterns.EMAIL_ADDRESS.matcher(email).matches(); } 
+5
source share
1 answer

Ok, I found that the problem comes from classes in android.jar. These classes are mocked during the unit test (see Tools.android.com/tech-docs/unit-testing-support), and I have to use robolectric to test the class.

+5
source

All Articles