This is the same question posted on developer.salesfoce.

I've completed a method on XmlStreamReader, it is now working fine, can extract the data out correct. But the issue is with the test class coverage.

enter image description here

Below is my code:

Post method:

//Variables store data
public static String toUserName {get; set;}
public static String fromUserName {get; set;}
public static String msgType {get; set;}

    global static void doPost(){
        //Set up for XML processing
       RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        string strMsg = req.requestBody.toString();  
        System.debug('Request Contents' + strMsg);
        XmlStreamReader reader = new XmlStreamReader(strMsg);
            if(reader.getLocalName() == 'ToUserName'){
                    toUserName = reader.getText();
            else if(reader.getLocalName() == 'FromUserName'){
                reader.next(); //failed from here below
                    fromUserName = reader.getText();
            else if(reader.getLocalName() == 'MsgType'){
                    msgType = reader.getText();

Test Method:

static testMethod void testDoPost(){
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        String strTmp = '<xml><ToUserName>tUN</ToUserName><FromUserName>fUN</FromUserName><MsgType>mT</MsgType></xml>';
        req.requestBody = Blob.valueOf(strTmp);
        req.requestURI = 'https://ap1.salesforce.com/services/apexrest/Demo//CoreService';
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;
        XmlStreamReader reader = new XmlStreamReader(req.requestBody.toString());
        System.assert(ApexRESTCycle.fromUName == 'fUN');
        System.assert(ApexRESTCycle.toUName == 'tUN');
        System.assert(ApexRESTCycle.mType == 'mT');

The issue now is in the Post method, only the first node got covered, other nodes are not covered, error attached on above.

1 Answer 1


Got this working. In case this might help with others, just post the code and troubleshooting steps out.

I have replicated the Post in Workbench, then found out the same issue in error message. Althought I am still not very sure why in real, all XML elements posted correctly? Welcome if any explaination for this point.

Then I looked into my XML data structure and found out for every node, there is a start element with opening tag, like <ToUserName> or <FromUserName>. That was what was missing in original code, so I added them up.

This is my revised code:

            if(reader.getEventType() == XmlTag.START_ELEMENT && reader.getLocalName() == 'ToUserName'){
                toUName = reader.getText();
            }if(reader.getEventType() == XmlTag.START_ELEMENT && reader.getLocalName() =='FromUserName'){
                fromUName = reader.getText();
            }if(reader.getEventType() == XmlTag.START_ELEMENT && reader.getLocalName() == 'MsgType'){
                mType = reader.getText();

Of course, I will add an extra condition to eliminate if the Character is not null just like previous did.

You must log in to answer this question.

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