You need to sign in to do that
Don't have an account?
shrey.tyagi88@tcs.com
Test Class for Apex HTTP Web service
Hi Everyone,
I have this apex rest web service , that returns the parametres of record when an external application sends the Id through the URL.
Apex Class:
@RestResource(urlMapping='/v1/cans/*')
global with sharing class MyRestResource {
@HttpGet
global static CanWrapper doGet() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
CanWrapper response= new CanWrapper();
String CanId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
List<CAN__c> result = [SELECT Awarded_Project__c, Opportunity__r.Proposal__c,Opportunity__r.Account.Name, Opportunity__r.Master_IDIQ_Project__c FROM CAN__c WHERE Id = :CanId];
if(result.size()>0){
response.canrecord=result[0];
response.status='Success';
response.message='Callout Sucess';
}
else{
response.status='Fail';
response.message='No Record Found';
}
return response;
}
global class CanWrapper {
public CAN__c canrecord;
public String status;
public String message;
}
}
Apex Test
@isTest
private class Test_MyRestResource{
static testMethod void testDoGet() {
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = 'https://cs11.salesforce.com/services/apexrest/v1/cans/'+'a0DZ000000V5SjqMAF';
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response = res;
Test.startTest();
MyRestResource.CanWrapper results = MyRestResource.doGet();
Test.stopTest();
System.assertEquals('Success', results.status);
System.assertEquals('Callout Sucess', results.message);
}
}
Even though I am giving a legit Id in my test class , it is not returning a record. It is returning null and the method fails. Because of that if(result.size()>0) condition is not getting evaluated to True. Can anyone please help me with this???
Thanks a lot for your time
Shrey Tyagi
I have this apex rest web service , that returns the parametres of record when an external application sends the Id through the URL.
Apex Class:
@RestResource(urlMapping='/v1/cans/*')
global with sharing class MyRestResource {
@HttpGet
global static CanWrapper doGet() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
CanWrapper response= new CanWrapper();
String CanId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
List<CAN__c> result = [SELECT Awarded_Project__c, Opportunity__r.Proposal__c,Opportunity__r.Account.Name, Opportunity__r.Master_IDIQ_Project__c FROM CAN__c WHERE Id = :CanId];
if(result.size()>0){
response.canrecord=result[0];
response.status='Success';
response.message='Callout Sucess';
}
else{
response.status='Fail';
response.message='No Record Found';
}
return response;
}
global class CanWrapper {
public CAN__c canrecord;
public String status;
public String message;
}
}
Apex Test
@isTest
private class Test_MyRestResource{
static testMethod void testDoGet() {
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = 'https://cs11.salesforce.com/services/apexrest/v1/cans/'+'a0DZ000000V5SjqMAF';
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response = res;
Test.startTest();
MyRestResource.CanWrapper results = MyRestResource.doGet();
Test.stopTest();
System.assertEquals('Success', results.status);
System.assertEquals('Callout Sucess', results.message);
}
}
Even though I am giving a legit Id in my test class , it is not returning a record. It is returning null and the method fails. Because of that if(result.size()>0) condition is not getting evaluated to True. Can anyone please help me with this???
Thanks a lot for your time
Shrey Tyagi
The problem here is you can't access org data in your test class. For any test class, you must create test data in your test class method. Check out this Apex test class Trailhead module https://developer.salesforce.com/trailhead/module/apex_testing which helps you write a proper test class
All Answers
Salesforce today provides a HTTPMockCallout Class to test our service.
Please go through the below documentation, hope that helps.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
PLEASE SELECT THIS AS THE ANSWER, IF YOU LIKE IT.
Thanks,
Rohit Alladi
The problem here is you can't access org data in your test class. For any test class, you must create test data in your test class method. Check out this Apex test class Trailhead module https://developer.salesforce.com/trailhead/module/apex_testing which helps you write a proper test class