function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Helen Y.Helen Y. 

XmlStreamReader test class error: Cannot User getText on START_ELEMENT

Hi Experts,

This is the first time I am working on xml, please give some help. 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.

Error:

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();
                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(ApexRESTCycleExample.fromUName == 'fUN');
        System.assert(ApexRESTCycleExample.toUName == 'tUN');
        System.assert(ApexRESTCycleExample.mType == 'mT');
}

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

I need a perfect coverage, please help.

Thanks,
Helen
    Helen Y.Helen Y.
    Found out the missing puzzle. I have also posted the answer on Stack Exchange just in case someone might have the same question on it.
    http://salesforce.stackexchange.com/questions/122662/xmlstreamreader-test-method-error-cannot-user-gettext-on-start-element/122840#122840