简体   繁体   English

NPE 使用 mockito 进行单元测试

[英]NPE on unit test using mockito

I tried to unit test some util class, below :我试图对一些 util 类进行单元测试,如下所示:

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@gmail.com";

@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谢谢

Just solved it by switching from Patterns.EMAIL_ADRESS.matcher() to PatternsCompat.EMAIL_ADRESS.matcher() .只要解决了它由开关Patterns.EMAIL_ADRESS.matcher()PatternsCompat.EMAIL_ADRESS.matcher() That should work.那应该工作。

I was getting an NPE because Patterns.EMAIL_ADDRESS was null in my unit tests.我得到了 NPE,因为Patterns.EMAIL_ADDRESS在我的单元测试中为空。 Switching to android.support.v4.util.PatternsCompat solved that issue切换到android.support.v4.util.PatternsCompat解决了这个问题

您是否检查了以下结果:

 Matcher m = Patterns.EMAIL_ADDRESS.matcher(email);

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();
}

Rather than Mocking Patterns you should get expected output.您应该获得预期的输出,而不是模拟模式。

Recommend to use Robolectric推荐使用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("android.developer@gmail.com").matches()).isTrue();
    }

    @Test
    public void email_passedInvalidEmail_error() {
        assertThat(Patterns.EMAIL_ADDRESS.matcher("android.developer@gmail.com@in").matches()).isFalse();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM