I have created a mock response to gain coverage for a hhtp request, as per the following documentation:
Here is my class:
public class SASProposalGenerationClass {
private final Opportunity oppty;
@AuraEnabled
public static String getUIThemeDescription() {
String theme = UserInfo.getUiThemeDisplayed();
return theme;
}
public SASProposalGenerationClass(ApexPages.StandardController stdController){
this.oppty = (Opportunity)stdController.getRecord();
}
@AuraEnabled
public static Opportunity getParentOpportunity(String opportunityId){
if(opportunityId == null){
return null;
}
Opportunity o = [SELECT Id, Name, Owner.Name, Owner.Email, Vertical_Group__c, Billing_Entity__r.Name, Billing_Entity__r.Client_Account__c, Billing_Entity__r.Client_Account__r.Name, Billing_Entity__c, Billing_Entity__r.ABN__c, Billing_Entity__r.ACN__c, Campaign_Start_Date__c, Campaign_End_Date__c, Account.Name, Billing_Entity__r.Legal_Entity__c, Billing_Entity__r.REA_ID__c, Billing_Entity__r.CID__c, Owner.SAS_ID__c, Owner.SAS_Ad_Trafficker__c, Owner.SAS_Ad_Trafficker__r.SAS_ID__c FROM Opportunity WHERE Id =:opportunityId];
return o;
}
@AuraEnabled
public static String getSASProposalId(String opportunityId, String reqBody) {
// Instantiate a new http object
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
String url = 'http://sf-to-sas-sandbox.au.cloudhub.io/opportunities/' + opportunityId;
req.setEndpoint(url);
req.setMethod('POST');
req.setBody(reqBody);
req.setHeader('Content-Type', 'application/json');
// Send the request, and return a response
HttpResponse res = h.send(req);
System.debug('response:--> ' + res.getBody());
String responseString = String.valueOf(res.getBody());
return responseString;
}
}
and my test class:
@isTest
public class SASProposalGenerationClass_Test {
static testMethod void testSASProposalGeneration() {
//Need an agency Account
Account agency = DataFactory.generateSingleAccount();
INSERT agency;
//Need a contact
Contact contact = DataFactory.generateSingleContact();
contact.AccountId = agency.Id;
INSERT contact;
//Need an advertiser account
Account advertiser = DataFactory.generateSingleAccount();
INSERT advertiser;
//Need a billing Entity
SAGE_Billing_Details__c BE = DataFactory.generateSingleBillingEntity();
BE.Billing_Account__c = agency.id;
BE.Client_Account__c = advertiser.Id;
BE.Contact__c = contact.Id;
INSERT BE;
//Need an opportunuty
Opportunity op = DataFactory.generateSingleOpportunity();
op.Billing_Entity__c = BE.Id;
op.AccountId = advertiser.Id;
//set opportunity record type to Media Opportunity
List<RecordType> rtList = new List<RecordType>([SELECT ID FROM RecordType WHERE Name = 'Media Opportunity' AND SObjectType = 'Opportunity' LIMIT 1]);
op.RecordTypeId = rtList[0].Id;
INSERT op;
Test.startTest();
//Aura Component environment test
String currentTheme = SASProposalGenerationClass.getUIThemeDescription();
//get all opportunity details
Opportunity currentOp = SASProposalGenerationClass.getParentOpportunity(op.Id);
//set test mock http callout
Test.setMock(HttpCalloutMock.class, new SASProposalGenerationClass_MockResponse());
String res = SASProposalGenerationClass.getSASProposalId(op.Id, '{test:test}');
//VF page test environment
ApexPages.currentPage().getParameters().put('id',op.Id);
ApexPages.StandardController stdOpp = new ApexPages.StandardController(op);
SASProposalGenerationClass objSASProposalGenerationClass = new SASProposalGenerationClass(stdOpp);
Test.stopTest();
}
}
and finally my mock response:
@isTest
global class SASProposalGenerationClass_MockResponse implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest req) {
// Optionally, only send a mock response for a specific endpoint
// and method.
//System.assertEquals('http://sf-to-sas-sandbox.au.cloudhub.io/opportunities/' + opportunityId, req.getEndpoint());
System.assertEquals('POST', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"proposalId":"001827262524"}');
res.setStatusCode(200);
return res;
}
}
Via the test class the SASProposalGenerationClass has 95% coverage, however the Mock HTTP Response is being given 0% coverage, bringing down my org average.
Is there a way to resolve this issue? Considering the main class is getting coverage on the callout I can assume that the mock is being run, however it is showing 0% coverage which is confusing me?