My Mock class is not being called in the test class. So it is throwing 'Methods defined as TestMethod do not support Web service callouts' error.
PremCalloutServiceTest - This is the test class
@isTest
private class PremCalloutServiceTest {
//Implement mock callout tests here
@testSetup static void testData() {
Opportunity opps1 = new Opportunity();
opps1.Amount = 100;
opps1.Name = 'Opportunity 1';
opps1.type = 'New Policy';
opps1.CloseDate = Date.today();
opps1.StageName = 'Prospecting';
insert opps1;
Opportunity opps2 = new Opportunity();
opps2.Amount = 200;
opps2.Name = 'Opportunity 2';
opps2.type = 'New Policy';
opps2.CloseDate = Date.today();
opps2.StageName = 'Closed Won';
insert opps2;
}
@isTest
public static void passTest(){
Opportunity opp = [Select Id from Opportunity where Name='Opportunity 1' limit 1];
opp.StageName = 'Closed Won';
List<Opportunity> oppIds = new List<Opportunity>();
oppIds.add(opp);
Test.setMock(HttpCalloutMock.class, new PremCalloutServiceMock());
Test.startTest();
update oppIds;
// I have a trigger which will update stage name of the opportunity based response from
//callout method
Test.stopTest();
Opportunity oppTest = [Select Id,StageName from Opportunity where Id=:opp.Id limit 1];
system.assertEquals('Paying', oppTest.StageName);
}
}
PremCalloutServiceMock - This is the mock class
global class PremCalloutServiceMock implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest request) {
//System.assertEquals('callout:ProjectService', request.getEndpoint());
//System.assertEquals('POST', request.getMethod());
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody('OK');
response.setStatusCode(201);
return response;
}
}
PremCalloutService - This is the callout class
public class PremCalloutService {
//Complete the implementation for business and callout logic
@InvocableMethod(label='Prem Rest Callout')
public static void PostOpportunityToPrem(List<Id> oppIds){
Opportunity opp = [Select Id,Name,AccountId,CloseDate,Amount from Opportunity where Id in :oppIds limit 1];
WrapData payLoad = new WrapData();
payLoad.OppId = opp.Id;
payLoad.OppName = opp.Name;
payLoad.OppAccountId = opp.AccountId;
payLoad.OppCloseDate = opp.CloseDate;
payLoad.OppAmount = opp.Amount;
ID jobID = System.enqueueJob(new QueueablePremCall(payLoad));
system.debug(jobID);
}
public class QueueablePremCall implements Queueable, Database.AllowsCallouts {
WrapData payLoad;
QueueablePremCall(WrapData payLoad){
this.payLoad = payLoad;
}
public void execute(QueueableContext context) {
String jsonData = JSON.serialize(this.payLoad);
system.debug(jsonData);
String payloadID = this.payLoad.OppId;
String token;
if(!Test.isRunningTest()){
token = ServiceTokens__c.getValues('ProjectServiceToken').Token__c;
}
else{
token = 'qwertyuiopnjhgft';
}
PremCalloutService.makeCallOut(token,jsonData,payLoadId);
}
}
public static void makeCallOut(string token, String jsonData, String payloadID){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:ProjectService');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
request.setHeader('token',token);
request.setBody(jsonData);
HttpResponse response = http.send(request);
system.debug('response'+response.getStatusCode());
Opportunity opp = [Select Id, StageName from Opportunity where id=:payloadID];
if(response.getStatusCode() == 201) {
opp.StageName = 'Paying';
system.debug('201');
} else if(response.getStatusCode() == 500){
opp.StageName = 'Reject';
system.debug('500');
} else{
system.debug('elseeee'+ response.getStatusCode());
system.debug('elseeee'+ response.getBody());
}
system.debug(opp);
update opp;
}
public class WrapData{
public String OppId;
public String OppName;
public String OppAccountId;
public Date OppCloseDate;
public Decimal OppAmount;
}
}
Any help would be appreciated