0

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

7
  • If your callout is using a wsdl2apex generated class, you need to use a different type of interface for your mock. See: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Mar 23 at 6:50
  • @user254875486 , no it is not wsdl2apex generated class. I added my callout class Mar 23 at 6:58
  • Do you have any other test methods that also run that are not included in this post maybe? Mar 23 at 7:03
  • Maybe it's from the opportunity that's inserted with stage closed won in the testSetup method... I'm not sure. Can you try to move all of the code from testSetup to the actual test method? Mar 23 at 7:06
  • @user254875486. Thank You!!. I moved all the code from testSetup to test method itself and it worked. But I am not sure why though. Thank You!! Mar 23 at 7:13

1 Answer 1

0

As pointed out by user254875486, the mockout class was not being called because, the rest method was executed by the trigger action due to the insert operation done on the TestSetup method, instead of the actual test method where the mock was set. I corrected the trigger operation and it is fixed now.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .