I have some code in my catch block for both @Post
and @Get
methods. I need to cover catch block to get to 75% for GET, got catch block covered for POST
Here is an example :
// POST
my JSON :
[
{
"somevalue" : "abcd",
"customerId" : "12345"
},
{
"somevalue" : "xyz",
"customerId" : "78901"
}
]
I have created an API class for the above.
Logic has been handled in main class with try and catch block below
Main class :
@RestResource(urlmapping='/someendpoint/*')
global with sharing class Sample
{
public static String responseJson = '';
@HttpPost
global static void doObjectCreation()
{
// local variables
Boolean isException = false;
String requestJson;
RestRequest request = RestContext.request;
RestResponse response = RestContext.response;
requestJson = request.requestBody.toString();
try
{
String agencyCode = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
system.debug('agencyCode @@'+agencyCode);
list<creationAPI> qcAPI = creationAPI.parse(requestJson);
system.debug('request @@'+qcAPI);
// check incoming api is valid
if(qcAPI != null){ // DO Business logic and send response }
}
catch(Exception ex)
{
system.debug(' Exception found @@ ' + ex.getMessage());
isException = true;
CreateLogs.insertLogRec('Log Exception Logs Rec Type', '', '',
'doObjectCreation', (responseJson.length() > 32768) ? responseJson.substring(0,32767) : responseJson,
String.valueOf(''), true,'', ex);
// DO some business logic and send response
}
}
}
Test Class :
@isTest
public class Sample_Test {
@testSetup
public static void setup() {// insert objects here}
@isTest
public static void ObjectCreation()
{
list<creationAPI> myApi = creationAPI_Test.testParse(); // returns API list
Sample_Test.restLogic(myApi,null);
Test.startTest();
Sample.doObjectCreation();
Test.stopTest();
}
@isTest
public static void testwithExcepJSON()
{
try{
system.debug('Inside JSON Excep');
String myExcepJson = '';
QSample_Test.restLogic(null,myExcepJson);
Test.startTest();
Sample.doObjectCreation();
Test.stopTest();
}
catch(Exception myException){
system.debug('inside exception test'+myException.getTypeName());
}
}
public static void restLogic(list<creationAPI> theApi, String theExcepJson){
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/someendpoint/1010101';
req.httpMethod = 'POST';
req.requestBody = (theApi != null) ? Blob.valueOf(JSON.serializePretty(theApi)) :
(theExcepJson != null) ? Blob.valueOf(theExcepJson) : null ;
RestContext.request = req;
RestContext.response = res;
}
}
The above class covers my catch block and working as expected. I was able to cover for Post as it had requestBody , but not able to cover for Get.
Here is the example :
// GET
// NO request Body
Main class :
@RestResource(urlmapping='/GetEndpoint/*')
global with sharing class SampleGET
{
public static String responseJson = '';
@HttpGet
global static void doObjectFetch()
{
// local variables
Boolean isException = false;
RestRequest request = RestContext.request;
RestResponse response = RestContext.response;
try
{
String agencyCode = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
system.debug('agencyCode @@'+agencyCode);
// DO Business logic and send response
}
catch(Exception ex)
{
system.debug(' Exception found @@ ' + ex.getMessage());
isException = true;
CreateLogs.insertLogRec('Log Exception Logs Rec Type', '', '',
'doObjectFetch', (responseJson.length() > 32768) ? responseJson.substring(0,32767) : responseJson,
String.valueOf(''), true,'', ex);
// DO some business logic and send response
}
}
}
Test Class :
@isTest
public class SampleGET_Test {
@testSetup
public static void setup() {// insert objects here}
// this one works and covers code
@isTest
public static void ObjectRetrieval()
{
SampleGET_Test.restLogic('2020202');
Test.startTest();
SampleGET.doObjectFetch();
Test.stopTest();
}
@isTest
public static void testwithExcepJSON()
{
try{
Test.startTest();
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
//actual end point is : GetEndpoint
// to get exception I'm giving dummy end point - dummyUrl
req.requestURI = '/services/apexrest/dummyUrl/1010101';
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response = res;
SampleGET.doObjectFetch();
Test.stopTest();
}
catch(Exception myException){
system.debug('inside exception test'+myException.getTypeName());
}
}
public static void restLogic(String agencyCode){
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/GetEndpoint/'+agencyCode;
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response = res;
}
}
I'm not sure what I need to do in that exception method for Get.