Reputation: 308
I tried to unit test some util class, below :
public final class WalletInputValidationUtils {
private WalletInputValidationUtils() {
}
public static boolean isEmailValid(CharSequence email) {
return Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
}
and this is the test class
@RunWith(PowerMockRunner.class)
public class WalletInputValidationUtilsTest {
private static final CharSequence DUMMY_EMAIL = "[email protected]";
@Test
public void isEmailValidTest1() {
Assert.assertTrue(isEmailValid(DUMMY_EMAIL));
}
}
I got null pointer exception in this line
return Patterns.EMAIL_ADDRESS.matcher(email).matches();
this is the stack trace
java.lang.NullPointerException
at kudo.mobile.app.wallet.backwardcompatibility.WalletInputValidationUtils.isEmailValid(WalletInputValidationUtils.java:14)
at kudo.mobile.app.wallet.backwardcompatibility.WalletInputValidationUtilsTest.isEmailValidTest(WalletInputValidationUtilsTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:316)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:300)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunne
Can anyone help me to fix this ? thanks
Upvotes: 1
Views: 1323
Reputation: 180
Rather than Mocking Patterns you should get expected output.
Recommend to use Robolectric
Code snippet is below:
import android.os.Build;
import android.util.Patterns;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
@RunWith(RobolectricTestRunner.class)
@Config(sdk = Build.VERSION_CODES.P)
public class ExampleUnitTest {
@Test
public void mobileNumber_passedValidNumber_success() {
assertThat(Patterns.PHONE.matcher("+919895123456").matches()).isTrue();
}
@Test
public void mobileNumber_passedInvalidNumber_error() {
assertThat(Patterns.PHONE.matcher("+91+9895123456").matches()).isFalse();
}
@Test
public void email_passedValidEmail_success() {
assertThat(Patterns.EMAIL_ADDRESS.matcher("[email protected]").matches()).isTrue();
}
@Test
public void email_passedInvalidEmail_error() {
assertThat(Patterns.EMAIL_ADDRESS.matcher("[email protected]@in").matches()).isFalse();
}
}
Upvotes: 0
Reputation: 696
Just solved it by switching from Patterns.EMAIL_ADRESS.matcher()
to PatternsCompat.EMAIL_ADRESS.matcher()
. That should work.
Upvotes: 13
Reputation: 475
I was getting an NPE because Patterns.EMAIL_ADDRESS
was null in my unit tests. Switching to android.support.v4.util.PatternsCompat
solved that issue
Upvotes: 1
Reputation: 76779
think it should match alike that:
public static boolean isEmailValid(String email) {
Pattern pattern = Pattern.compile(Patterns.EMAIL_ADDRESS);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
Upvotes: 0