1

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;}

@HttpPost
    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);
        //Deserialize
        while(reader.hasNext()){
            if(reader.getLocalName() == 'ToUserName'){
                reader.next();
                if(String.isNotBlank(reader.getText())){
                    toUserName = reader.getText();
                }
            }
            else if(reader.getLocalName() == 'FromUserName'){
                reader.next(); //failed from here below
                if(String.isNotBlank(reader.getText())){
                    fromUserName = reader.getText();
                }
            }
            else if(reader.getLocalName() == 'MsgType'){
                reader.next();
                if(String.isNotBlank(reader.getText())){
                    msgType = reader.getText();
                }
            }
            reader.next();
        }
    }

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';
        test.startTest();
        RestContext.request = req;
        RestContext.response = res;
        ApexRESTCycle.doPost();
        XmlStreamReader reader = new XmlStreamReader(req.requestBody.toString());
        test.stopTest();
        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.

0

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:

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

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

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.