ProjectCalloutServiceMock :- ========================= @isTest global class ProjectCalloutServiceMock implements HttpCalloutMock{ //Implement http mock callout here // Implement this interface method global HTTPResponse respond(HTTPRequest request){ // Create a fake response HttpResponse response = new HttpResponse(); response.setHeader('Content-Type', 'application/json'); response.setStatus('OK'); response.setStatusCode(201);
return response; } } ========================================================================== ProjectCalloutServiceMockFailure:- --------------------------------------------------------- @isTest global class ProjectCalloutServiceMockFailure implements HttpCalloutMock{ //Implement http mock callout here // Implement this interface method global HTTPResponse respond(HTTPRequest request){ // Create a fake response HttpResponse response = new HttpResponse(); response.setHeader('Content-Type', 'application/json'); response.setStatus('Bad Response'); response.setStatusCode(500);
return response; } } =========================================================================== ProjectCalloutServiceTest :- ---------------------------------------------- @isTest private class ProjectCalloutServiceTest { //Implement mock callout tests here @testSetup static void testSetupdata(){ //create the opportunity record Opportunity opp1 = new Opportunity(); opp1.Name = 'Test Opp1'; opp1.Type = 'New Project'; opp1.Amount = 100; opp1.CloseDate = Date.today(); opp1.StageName = 'Submitted Project'; insert opp1; //create the opportunity record Opportunity opp2 = new Opportunity(); opp2.Name = 'Test Opp2'; opp2.Type = 'New Project'; opp2.Amount = 200; opp2.CloseDate = Date.today(); opp2.StageName = 'Resubmit Project'; insert opp2; //create the Custom Settings ServiceTokens__c servToken = new ServiceTokens__c(); servToken.Name = 'ProjectServiceToken'; servToken.Token__c = 'qwertyuiopnjhgft'; insert servToken; }
@isTest static void testSuccessMessage(){ Opportunity opp = [Select Id, Name FROM Opportunity WHERE Name = 'Test Opp1' Limit 1]; List lstOfOppIds = new List(); lstOfOppIds.add(opp.Id); // Set mock callout class Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock()); // This causes a fake response to be sent // from the class that implements HttpCalloutMock. Test.startTest(); ProjectCalloutService.postOpportunityToPMS(lstOfOppIds); Test.stopTest(); // Verify that the response received contains fake values opp = [select StageName from Opportunity where id =: opp.Id]; System.assertEquals('Submitted Project',opp.StageName); }
@isTest static void testFailureMessage(){ Opportunity opp = [Select Id, Name FROM Opportunity WHERE Name = 'Test Opp2' Limit 1]; List lstOfOppIds = new List(); lstOfOppIds.add(opp.Id); // Set mock callout class Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMockFailure()); // This causes a fake response to be sent // from the class that implements HttpCalloutMock. Test.startTest(); ProjectCalloutService.postOpportunityToPMS(lstOfOppIds); Test.stopTest(); // Verify that the response received contains fake values opp = [select StageName from Opportunity where id =: opp.Id]; System.assertEquals('Resubmit Project',opp.StageName); } }
ProjectCalloutServiceMock :-
=========================
@isTest
global class ProjectCalloutServiceMock implements HttpCalloutMock{
//Implement http mock callout here
// Implement this interface method
global HTTPResponse respond(HTTPRequest request){
// Create a fake response
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setStatus('OK');
response.setStatusCode(201);
return response;
}
}
==========================================================================
ProjectCalloutServiceMockFailure:-
---------------------------------------------------------
@isTest
global class ProjectCalloutServiceMockFailure implements HttpCalloutMock{
//Implement http mock callout here
// Implement this interface method
global HTTPResponse respond(HTTPRequest request){
// Create a fake response
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setStatus('Bad Response');
response.setStatusCode(500);
return response;
}
}
===========================================================================
ProjectCalloutServiceTest :-
----------------------------------------------
@isTest
private class ProjectCalloutServiceTest {
//Implement mock callout tests here
@testSetup static void testSetupdata(){
//create the opportunity record
Opportunity opp1 = new Opportunity();
opp1.Name = 'Test Opp1';
opp1.Type = 'New Project';
opp1.Amount = 100;
opp1.CloseDate = Date.today();
opp1.StageName = 'Submitted Project';
insert opp1;
//create the opportunity record
Opportunity opp2 = new Opportunity();
opp2.Name = 'Test Opp2';
opp2.Type = 'New Project';
opp2.Amount = 200;
opp2.CloseDate = Date.today();
opp2.StageName = 'Resubmit Project';
insert opp2;
//create the Custom Settings
ServiceTokens__c servToken = new ServiceTokens__c();
servToken.Name = 'ProjectServiceToken';
servToken.Token__c = 'qwertyuiopnjhgft';
insert servToken;
}
@isTest
static void testSuccessMessage(){
Opportunity opp = [Select Id, Name FROM Opportunity WHERE Name = 'Test Opp1' Limit 1];
List lstOfOppIds = new List();
lstOfOppIds.add(opp.Id);
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock());
// This causes a fake response to be sent
// from the class that implements HttpCalloutMock.
Test.startTest();
ProjectCalloutService.postOpportunityToPMS(lstOfOppIds);
Test.stopTest();
// Verify that the response received contains fake values
opp = [select StageName from Opportunity where id =: opp.Id];
System.assertEquals('Submitted Project',opp.StageName);
}
@isTest
static void testFailureMessage(){
Opportunity opp = [Select Id, Name FROM Opportunity WHERE Name = 'Test Opp2' Limit 1];
List lstOfOppIds = new List();
lstOfOppIds.add(opp.Id);
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMockFailure());
// This causes a fake response to be sent
// from the class that implements HttpCalloutMock.
Test.startTest();
ProjectCalloutService.postOpportunityToPMS(lstOfOppIds);
Test.stopTest();
// Verify that the response received contains fake values
opp = [select StageName from Opportunity where id =: opp.Id];
System.assertEquals('Resubmit Project',opp.StageName);
}
}