replaceAll()在模拟此行时在Java中引发NullPointerException

时间:2019-06-22 11:47:59

标签: java spring mockito powermockito

当我尝试模拟以下代码时,我得到nullpointerexception 我试图调用webtarget,并且URL具有用户ID。需要在运行时用登录的用户ID替换该用户ID。请参阅下面的代码作为参考

Java代码:

webTarget = oAuthHelper.createWebTarget(cleanTokenUrlTemp);
                URI uri = webTarget.getUri();
                String webTargetUri = uri.toString();


                Pattern pattern = Pattern.compile(USER_ID);
                Matcher matcher = pattern.matcher(webTargetUri); 
                String userID=matcher.replaceAll(matcher.quoteReplacement(userId));

测试类:PowerMock尝试对代码进行覆盖以模拟代码。

java.net.URI uri = new java.net.URI(
            "https://localhost:8080/common/user/userid/");
        Mockito.when(mockWebTarget1.getUri()).thenReturn(uri);
        String Uri = uri.toString();

        Mockito.when(oAuthHelper.createWebTarget(anyString())).thenReturn(mockWebTarget1);
        Mockito.when(mockWebTarget1.request(Mockito.anyString(), Mockito.anyString())).thenReturn(mockBuilder);
        when(mockBuilder.header(anyString(), anyString())).thenReturn(mockbuilder1);
        when(mockbuilder1.header(anyString(), anyString())).thenReturn(mockbuilder2);
        when(mockbuilder2.post(any(Entity.class))).thenReturn(mockResponse);
        Mockito.when(mockResponse.readEntity(Response.class)).thenReturn(response);
        final String userId = "deepa";

        Pattern pattern = Pattern.compile("userid");
        Matcher matcher = pattern.matcher(Uri);
        Uri = matcher.replaceAll(matcher.quoteReplacement("deepa"));

        LoginServiceImpl.getInfo("userid", userId);

例外:

java.lang.NullPointerException
    at java.util.regex.Matcher.appendReplacement(Unknown Source)
    at java.util.regex.Matcher.replaceAll(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
    at com.frk.intl.usermgmt.profile.service.impl.PingLogoutServiceImpl.apigeeClearToken(PingLogoutServiceImpl.java:213)
    at com.frk.intl.usermgmt.profile.service.impl.PingLogoutServiceImpl.cookieToken(PingLogoutServiceImpl.java:183)
    at com.frk.intl.usermgmt.profile.service.impl.PingLogoutServiceImpl.pingLogout(PingLogoutServiceImpl.java:114)
    at com.frk.intl.common.application.impl.test.PingLogoutServiceImplTest.getPingLogoutTest(PingLogoutServiceImplTest.java:232)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

0 个答案:

没有答案