1

How can I write a test class with DOM.XMLNode response?

request.setBody('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Header/><Body><login xmlns="urn:partner.soap.sforce.com"><username>' + '<userName>' + '</username><password>' + '<Password>' + '</password></login></Body></Envelope>');
    Dom.XmlNode resultElmt = (new Http()).send(request).getBodyDocument().getRootElement()
            .getChildElement('Body','http://schemas.xmlsoap.org/soap/envelope/')
            .getChildElement('loginResponse','urn:partner.soap.sforce.com')
            .getChildElement('result','urn:partner.soap.sforce.com');

    String sessionId = resultElmt.getChildElement('sessionId','urn:partner.soap.sforce.com').getText();

Test class from comment:

@IsTest 
global class Stubby implements HttpCalloutMock { 
    global HTTPResponse respond(HTTPRequest req) { 
        HttpResponse res = new HttpResponse();
        Dom.Document doc = res.getBodyDocument();
        Dom.XMLNode address = doc.getRootElement();
        String name = address.getChildElement('serverUrl', null).getText();
        String SESSION_ID = address.getChildElement('SESSION_ID', null).getText();
    }
}

1 Answer 1

1

It looks like you are doing a raw HTTP HttpRequest to the Partner API to establish a Session. In this case you create the test using an HttpCalloutMock.

With you Http Mock you can return whatever HTTPResponse you need.

A sample SOAP envelope response from the login method is available in PartnerLogin.


Your class that implements HttpCalloutMock needs to return the HttpResponse with a body that would otherwise come from the real service.

    string sessionBody = '<?xml version="1.0" encoding="utf-8"?>' +
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"' +
  'xmlns="urn:partner.soap.sforce.com"' +
  'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
  '<soapenv:Body>' +
     '<loginResponse>' +
        '<result>' +
           '<passwordExpired>false</passwordExpired>' +
           '<serverUrl>https://na1.salesforce.com/services/Soap/u/10.0</serverUrl>' +
           '<sessionId>QwWsHJyTPW.1pd0_jXlNKOSU</sessionId>' +
           '<userId>005D0000000nVYVIA2</userId>' +
           '<userInfo>' +
              '<!-- ... -->' +
           '</userInfo>' +
        '</result>' +
     '</loginResponse>' +
  '</soapenv:Body>' +
'</soapenv:Envelope>';

    HttpResponse res = new HttpResponse();
    res.setHeader('Content-Type', 'text/xml; charset=utf-8');
    res.setBody(sessionBody);
    res.setStatusCode(200);
    return res;
7
  • Hi Daniel, I have added this code for HTTPRequest callouts. Please suggest what I need to do else?@IsTest global class Stubby implements HttpCalloutMock { global HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); Dom.Document doc = res.getBodyDocument(); Dom.XMLNode address = doc.getRootElement(); String name = address.getChildElement('serverUrl', null).getText(); String SESSION_ID = address.getChildElement('SESSION_ID', null).getText(); }
    – emy
    Commented Mar 27, 2017 at 7:56
  • @oookkk You need to include the expected SOAP response. Or at least enough of it so you can test parse the SessionId and ServerUrl out of it. Commented Mar 27, 2017 at 9:09
  • Hi Daniel, Do I need to include (string )session body of SOAP response in my test class that you have mentioned ?
    – emy
    Commented Mar 27, 2017 at 11:44
  • how can we parse this SOAP response in order to check in test class?
    – emy
    Commented Mar 29, 2017 at 12:31
  • @oookkk Honestly, you seem to be doing it the hard way by manually creating and parsing the SOAP response. I created an alternative version of wsdl2apex that will let you just import the login method. It can even generate the test class for coverage and the mock for you if needed. Commented Mar 29, 2017 at 18:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .