Writing TEST class for REST API
If you want to have a reference for how to write REST API, please use the below link.
http://priyamohanraj.blogspot.in/2014/05/calling-rest-api-in-salesforce.html
we should first write the MOCK Class for the http Request.
MOCK REQUEST FOR TEST CLASS
@isTest
global class Mockhttpcallouts implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req)
{
system.debug('------------------'+req);
HttpResponse res = new HttpResponse();
System.assertEquals('GET', req.getMethod());
res.setHeader('Content-Type', 'application/json');
res.setBody('[{"FirstName" : "test","LastName" : "data1","Email" : "test.data1@email.com","Title" : "Mr"},{"FirstName" : "test","LastName" : "data2","Email" : "test.data2@email.com","Title" : "Miss"},{"FirstName" : "test","LastName" : "data3","Email" : "test.data3@email.com","Title" : "Mr"},{"FirstName" : "test","LastName" : "data4","Email" : "test.data4@email.com","Title" : "Miss"},{"FirstName" : "test","LastName" : "data5","Email" : "test.data5@email.com","Title" : "Mr"}]');
system.debug('------------INSIDE IF-------------');
res.setStatusCode(200);
}
}
Now test class for the API Class
The above call will execute whenever Account record is insert, it will trigger this API call and get the contact data and insert the contact data into salesforce.
@isTest(seeAlldata =false)
public class TestExternal_APITrg
{
@isTest static void APIClass_Test()
{
Account ac = new Account();
ac.Name = 'Test Account';
ac.CountryName__c = 'India';
Insert ac;
Test.startTest();
Test.setMock(HttpCalloutMock.class, new Mockhttpcallouts());
try
{
APIClass.JSONAPICall(ac);
}
catch (System.CalloutException e){
System.debug('ERROR:' + e);
}
}
}
If you want to have a reference for how to write REST API, please use the below link.
http://priyamohanraj.blogspot.in/2014/05/calling-rest-api-in-salesforce.html
we should first write the MOCK Class for the http Request.
MOCK REQUEST FOR TEST CLASS
@isTest
global class Mockhttpcallouts implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req)
{
system.debug('------------------'+req);
HttpResponse res = new HttpResponse();
System.assertEquals('GET', req.getMethod());
res.setHeader('Content-Type', 'application/json');
res.setBody('[{"FirstName" : "test","LastName" : "data1","Email" : "test.data1@email.com","Title" : "Mr"},{"FirstName" : "test","LastName" : "data2","Email" : "test.data2@email.com","Title" : "Miss"},{"FirstName" : "test","LastName" : "data3","Email" : "test.data3@email.com","Title" : "Mr"},{"FirstName" : "test","LastName" : "data4","Email" : "test.data4@email.com","Title" : "Miss"},{"FirstName" : "test","LastName" : "data5","Email" : "test.data5@email.com","Title" : "Mr"}]');
system.debug('------------INSIDE IF-------------');
res.setStatusCode(200);
}
}
Now test class for the API Class
The above call will execute whenever Account record is insert, it will trigger this API call and get the contact data and insert the contact data into salesforce.
@isTest(seeAlldata =false)
public class TestExternal_APITrg
{
@isTest static void APIClass_Test()
{
Account ac = new Account();
ac.Name = 'Test Account';
ac.CountryName__c = 'India';
Insert ac;
Test.startTest();
Test.setMock(HttpCalloutMock.class, new Mockhttpcallouts());
try
{
APIClass.JSONAPICall(ac);
}
catch (System.CalloutException e){
System.debug('ERROR:' + e);
}
}
}
No comments:
Post a Comment