0

So, I am trying to mock a jersey get method and to some extent my test case is working fine but it throws NPE when it gets to get(Response.class) method. I've spent a lot of time but couldn't figure out.

Method under test (a few lines)

DocusignRESTClient client = (DocusignRESTClient) AppContext.getBean("restServiceClient");
        WebTarget resourcePing = client.createResource( ESignatureSpringUtil.getMessage( KeyConstants.ALSB_DOCUSIGN_ADDRESS )
                + ESignatureSpringUtil.getMessage( KeyConstants.REST_PING_VENDOR_ADDRESS ) );

        Response response = resourcePing
            .request()
            .accept( MediaType.APPLICATION_XML )
            .header( KeyConstants.REST_URI_APPENDERS, pingSb )
            .header( DocusignRESTContants.CONTENT_TYPE, MediaType.APPLICATION_JSON )
            .header( DocusignRESTContants.X_DOCUSIGN_AUTHENTICATION, getDocusignAuthHeader( cu ) )
            .get( Response.class );

Test Case

@Test
public void testPingVendor() throws Exception, DocusignRESTClientException {
    accountId = "025f1a5d-b796-4ba6-85d2-b2a4a90d109c";
    address = "docusignRestAddress";

    AppContext.setApplicationContext( applicationContext );
    iClientUserDto.setClientUserVendor( iClientUserVendorDto );
    DocusignRESTProvider docusignRestProvider = new DocusignRESTProvider();
    docusignRestProvider.setLoggingHandler( iloggingHandler );

    PowerMockito.when( DtoUtils.lookupClientUserVendorParam( any( String.class ), Mockito.anyListOf( IClientUserVendorParamDto.class ) ) ).thenReturn( accountId );
    PowerMockito.when( ESignatureSpringUtil.getMessage( any( String.class ) ) ).thenReturn( address );
    PowerMockito.when( AppContext.getBean( any( String.class ) ) ).thenReturn( docusignRestClient );
    PowerMockito.when( DocusignRESTUtil.getDocusignAuthHeader( iClientUserDto ) ).thenReturn( "authProvider" );

    when( docusignRestClient.createResource( any( String.class ) ) ).thenReturn( webTarget );
    when( webTarget.request() ).thenReturn( builder );
    when( webTarget.request().accept( any( MediaType.class ) ) ).thenReturn( builder );
    when( webTarget.request().accept( any( MediaType.class ) ).header( any( String.class ), anyObject() ) ).thenReturn( builder );
    when( webTarget.request().accept( any( MediaType.class ) ).header( any( String.class ), anyObject() ).get( Response.class ) ).thenReturn( response );

    docusignRestProvider.pingVendor( iClientUserDto );

stack trace:

java.security.ProviderException
at com.mercuryinsurance.esignature.integration.provider.docusign.rest.DocusignRESTProvider.pingVendor(DocusignRESTProvider.java:204)
at test.com.mercuryinsurance.esignature.integration.provider.docusign.rest.TestDocusignRESTProvider.testPingVendor(TestDocusignRESTProvider.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
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:84)
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:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
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:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NullPointerException
... 31 more
Mike
  • 777
  • 3
  • 16
  • 41
  • @Oliver Charlesworth I know what is NPE but here the third party library is involved and I am not sure what is causing this. Please don't mark duplicate if you don't know the answer. – Mike Apr 07 '17 at 21:32
  • But the process for identifying the cause is always the same. Debug, step through, etc. until you've hit the line that throws the underlying error, then work backwards. – Oliver Charlesworth Apr 07 '17 at 21:44
  • Do you think I haven't done those steps. Look at the get method which returns Response object. It is all in one line you can't even put a break point. For each method I have a mock method invocation. That one is the custom object. You haven't even read the question man. This is Jersey rest implementation. So simple to mark duplicate without even seeing anything. – Mike Apr 07 '17 at 21:47
  • You could split it into multiple statements. Or break immediately before, and then step through. – Oliver Charlesworth Apr 07 '17 at 21:48
  • break before what? – Mike Apr 07 '17 at 21:53
  • Sorry, I misunderstood what you meant by "it's all on one line" to mean the `resourcePing.accept(...)...` line. Either way, this is basically impossible for anyone to help with currently, because you've only pasted part of your method-under-test, part of your test code (what is e.g. `response`?), and there's not enough information to tell what part is actually throwing the exception(s). I suggest boiling this down to a [minimal test-case](http://stackoverflow.com/help/mcve). – Oliver Charlesworth Apr 07 '17 at 21:58
  • Method is really long and it got so many layers. It is blowing right in the get part. Response is the jersey Class. – Mike Apr 07 '17 at 22:06

0 Answers0