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.
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.