2

I have tried to write a test class for the below class with post method. But i am not able to bring the coverage.How can I approach this. Thanks in advance.

/* code starts here*/

@RestResource(urlMapping='/ABCAVM/Property/Analytics/')
global class ABCAVMService {
            @HttpPost
        global static ABCGlobalTypes.ABCStatusResponse doPo

st(){
            ABCGlobalTypes.ABCStatusResponse resp;

        //Handle incoming request
        RestRequest req = RestContext.request;
        Map<String,String> params = req.params;
        String requestData = req.requestBody.toString();

        //Check for blank request
        if(String.isBlank(requestData)){
            resp=new ABCGlobalTypes.ABCStatusResponse();
            resp.wsResponse = new ABCCommon.wsResponseType();
            resp.wsResponse.wmstatus = 'error';
            resp.wsResponse.wmmessage = 'Request body was empty';
            return resp;
        } 
        //Hand off request to main processing class
        ABCAVM avm = new ABCAVM();
        resp = new ABCGlobalTypes.ABCStatusResponse();
        resp.wsResponse = avm.insertAnalytics(requestData);

        return resp;

    }

}

This is my test Class

@isTest
private class Test_GNWAVMService {

static {
// setup test data  
}

static testMethod void testdoPost() {

String requestData = '1234';
GNWGlobalTypes.GNWStatusResponse app = new GNWGlobalTypes.GNWStatusResponse(); 
//new app.doPost();
//RestRequest req = new GNWGlobalTypes.GNWStatusRequest();
//RestResponse res = new GNWStatusResponse();
RestRequest sas = RestContext.request;
RestResponse res = new RestResponse();

//req.requestURI = 'https://cs9.salesforce.com/services/apexrest/v.9/member/me/results/today';  
//res.httpMethod = 'POST';
//RestContext.request = req;
RestContext.response = res;
RestContext.request= sas;

//GNWGlobalTypes.GNWStatusResponse results = GNWAVMService.doPost();

//System.assertEquals('true', results.success);
//System.assertEquals(10, results.records.size());
//System.assertEquals('Query executed successfully.', results.message);

}

}
1

1 Answer 1

2

Please see the below code you will get some Idea

@RestResource(urlMapping='/DemoUrl/*')

global with sharing class MyRestResourcedemo {

  global class RequestWrapper{
   public  Account acct;
    public Contact[] cons;
}

 global class ResponseWrapper {           
    public String StatusCode;
    public String StatusMessage;
    public Account acct;
    public Contact[] cons;    
}

@HttpPost
  global static ResponseWrapper doPost(RequestWrapper reqst) {

    ResponseWrapper resp = new ResponseWrapper();     
    try{
    insert reqst.acct;
    for(Contact c:reqst.cons){
    c.AccountId = reqst.acct.Id;
    }
    Upsert reqst.cons;
    }
    catch( Exception e ) {
            resp.statusCode = 'Error';
            resp.statusMessage = 'Exception : ' + e.getMessage();
       }
        resp.statusCode = 'Done';
        resp.statusMessage = 'Test success message';
        resp.acct = reqst.acct;
        resp.cons = reqst.cons;

    return resp;
  }
}

The test class for above code will be as below

@istest
public class SFA_TestRestPostService {

 static testMethod void  testPostRestService(){

   Account acc=new Account();
   acc.name='Test';
   acc.AccountNumber='1232332';
   acc.Site='site';
   acc.Website='cloudyworlds.blogspot.in';

   List<contact> lstcon=new List<contact>();

   integer i;
  for(i=0;i<=10;i++){
     Contact c=new Contact();
     c.lastname='Test+i';
     lstcon.add(c);
 }

  MyRestResourcedemo.RequestWrapper reqst=new     MyRestResourcedemo.RequestWrapper();
   reqst.acct=acc;
   reqst.cons=lstcon;

   String JsonMsg=JSON.serialize(reqst);

   Test.startTest();

  //As Per Best Practice it is important to instantiate the Rest Context 

  RestRequest req = new RestRequest(); 
   RestResponse res = new RestResponse();

    req.requestURI = '/services/apexrest/DemoUrl';  //Request URL
    req.httpMethod = 'POST';//HTTP Request Type
    req.requestBody = Blob.valueof(JsonMsg);
    RestContext.request = req;
    RestContext.response= res;



   MyRestResourcedemo.ResponseWrapper resp =new  
   MyRestResourcedemo.ResponseWrapper(); 
   resp=MyRestResourcedemo.doPost(reqst); //Call the Method of the Class with Proper       Constructor 
   System.assert(resp.statusMessage.contains('Test success message'));//Assert the response has message as expected 
    System.assert(resp.statusCode.contains('Done'));
    System.assert(resp.acct.Id!=null);//Assert that the Account is inserted and has Id
    Test.stopTest();

   }
}
1
  • Mohith -- I think the testmethod lines RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); need to be RestContext.request = new RestRequest(); RestContext.response = new RestResponse(); otherwise the code under test will get a null when referencing RestContext.request or RestContext.response
    – cropredy
    Oct 26, 2017 at 17:25

You must log in to answer this question.

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