0

I am trying to write a simple apex test class for the wsdl2apex file that i generated, but for some reason I am getting a null pointer exception in the test method which mocks the apex callout. I developed this with reference to the documentation at - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm The problem is in below class at line respElement.orderResponse.CorrelationID = '23145'; where orderResponse is an inner class. Here is my apex test classes:

@isTest
global class GcsmsWebmethodsMock implements WebServiceMock{

global void doInvoke(Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType){

  gcsmsWebmethodsNaJnjComMu650Gcsms.processOrderResponse respElement = new gcsmsWebmethodsNaJnjComMu650Gcsms.processOrderResponse();
  respElement.orderResponse.CorrelationID = '23145';
  response.put('response_x', respElement);


}

}

Class which calls webservice:

public class WebSvcCallout {
    public static gcsmsWebmethodsNaJnjComMu650Gcsms.orderResponse callEchoString(String input) {
    gcsmsWebmethodsNaJnjComMu650Gcsms.MU650_GCSMS_Order_v1_webservices_order_wsd_pro_Port sample = new gcsmsWebmethodsNaJnjComMu650Gcsms.MU650_GCSMS_Order_v1_webservices_order_wsd_pro_Port();
        sample.endpoint_x = 'http://api.salesforce.com/foo/bar';

        gcsmsWebmethodsNaJnjComMu650Gcsms.orderInput req = new gcsmsWebmethodsNaJnjComMu650Gcsms.orderInput();
        // This invokes the EchoString method in the generated class
        gcsmsWebmethodsNaJnjComMu650Gcsms.orderResponse res = sample.processOrder(req);

        return res;
    }   
}

The test class which calls the web service. here I get null pointer exception - attempted to de-reference null object(please see the comment below at which line)

@isTest
private class WebSvcCalloutTest {
    @isTest static void testEchoString() {              
        // This causes a fake response to be generated
        Test.setMock(WebServiceMock.class, new GcsmsWebmethodsMock());

        // Call the method that invokes a callout
        gcsmsWebmethodsNaJnjComMu650Gcsms.orderResponse output = WebSvcCallout.callEchoString('Hello World!'); // null pointer exception - //attempted to de-reference null object

        // Verify that a fake result is returned
        System.assertEquals('23145', output.CorrelationID); 
    }
}

1 Answer 1

1

Saving the communities time here. I have found the bug on my own. I did not initialize the object in my test class before using it. So the resolution should be to create an object of class orderResponse and use this object to assign a value to correlationID.

  gcsmsWebmethodsNaJnjComMu650Gcsms.processOrderResponse respElement = new gcsmsWebmethodsNaJnjComMu650Gcsms.processOrderResponse();
  gcsmsWebmethodsNaJnjComMu650Gcsms.orderResponse obj2 = new gcsmsWebmethodsNaJnjComMu650Gcsms.orderResponse();
  obj2.CorrelationID = '23145';
 respElement.orderResponse = obj2;

Above change to the first class solves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.