You need to sign in to do that
Don't have an account?
Hussein Azeez 2
Hi everyone, I would like to get some help on creating a testing class for a Webservice callout class
Here is the class that I want to create a test class for
I've tried to test it with CalloutWithStaticResources but it turned out that the class method declared as a void type. Here is the test class that I've tried to use:
The test is failed and I only got 18% coverage.
I will appreciate any kind of help. Thank you.
@RestResource(urlMapping='/delivery_report') global class SmsDeliveryReport { @HttpGet global static void receiveReport() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; if(req.params.containsKey('Transaction') && req.params.containsKey('Status')) { List<Task> updatedSms = updateDeliveryStatus(req.params.get('Transaction'), req.params.get('Status')); if(!updatedSms.isEmpty()) { res.statuscode = 200; res.addHeader('Content-Type', 'text/html'); res.responseBody = Blob.valueOf('REPORT_RECEIVED'); } else { res.statuscode = 500; res.addHeader('Content-Type', 'text/html'); res.responseBody = Blob.valueOf('FAILED_TO_RECEIVE_REPORT'); } } else { res.statuscode = 400; res.addHeader('Content-Type', 'text/html'); res.responseBody = Blob.valueOf('MISSING_REQUIRED_FIELD'); } } private static List<Task> updateDeliveryStatus(String transactionId, String newStatus) { List<Task> sms = [ SELECT SMS_Delivery_Status__c FROM Task WHERE SMS_Transaction_ID__c = :transactionId ORDER BY CreatedDate DESC LIMIT 1 ]; if(!sms.isEmpty()) { sms.get(0).SMS_Delivery_Status__c = newStatus; update sms; } return sms; } }
I've tried to test it with CalloutWithStaticResources but it turned out that the class method declared as a void type. Here is the test class that I've tried to use:
@IsTest public class SmsDeliveryReportTest { @isTest static void receiveReportTest() { // Use StaticResourceCalloutMock built-in class to // specify fake response and include response body // in a static resource. StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); mock.setStaticResource('mockResponse'); mock.setStatusCode(200); mock.setHeader('Content-Type', 'application/json'); // Set the mock callout mode Test.setMock(HttpCalloutMock.class, mock); HTTPResponse res = new HTTPResponse(); // Call the method that performs the callout SmsDeliveryReport.receiveReport(); // Verify response received contains values returned by // the mock response. // This is the content of the static resource. System.assertEquals('{"hah":"fooled you"}', res.getBody()); System.assertEquals(200,res.getStatusCode()); System.assertEquals('application/json', res.getHeader('Content-Type')); } }
The test is failed and I only got 18% coverage.
I will appreciate any kind of help. Thank you.
Try with following test class. I have removed the assersion. You may need to add return type to assert.
Please make sure to mark this as the best answer if this help you, as it could help more people in the community in future.