function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hormoz Hekmat 7Hormoz Hekmat 7 

Deployment error : Methods defined as TestMethod do not support Web service callouts Stack Trace: null

I wrote a class 
public with sharing class SlackOpportunityPush {
    
    
    // To publish Opportunity values in form of a String//
    public static final String slackURL = 'https://hooks.slack.com/services/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';
    
    public class Oppty {
        @InvocableVariable(label='Opportunity Name')
        public String opptyName;
        
        @InvocableVariable(label='Owner')
        public String Owner;
        
        @InvocableVariable(label='Opportunity Amount')
        public String Amount;
        
        @InvocableVariable(label='Account Development Owner')
        public String AccountDE;
        
        @InvocableVariable(label='Account')
        public String Account;
    }
     
    @InvocableMethod(label='Push to Slack')
    
    public static void postToSlack(List<Oppty> oppties) { 
        Oppty o = oppties[0]; // If bulk, only post first to avoid overloading Slack channel
        Map<String,Object> msg = new Map<String,Object>();
        msg.put('text',  '\n Opportunity Won !  Congrats to Opportunity Owner !  :-  ' +o.Owner +   '\n and Account Development Owner:-  '+ o.AccountDE + '\n who just closed Opportunity:-  ' +o.opptyName +   '\n associated to Account:- ' + o.Account +'\n for Opportunity Amount:- $ ' + o.Amount);
        
        System.debug('Called msg.put');
        System.debug('OpportunityOwner '+o.Owner);
        System.debug('OpportunityAccount '+o.Account);
        System.debug('OpportunityAmount '+o.Amount);
        
        
        msg.put('mrkdwn',true);
        String body = JSON.serialize(msg);    
        System.enqueueJob(new QueueableSlackCall(slackURL,'POST',body));
    
    
    
    }
     
    public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
         
        public final String url;
        public final String method;
        public final String body;
         
        public QueueableSlackCall(String url,String method,String body) {
            this.url = url;
            this.method = method;
            this.body = body;
        }
         
        public void execute(System.QueueableContext ctx) {
            HttpRequest req = new HttpRequest();
            req.setEndpoint(url);
            req.setMethod(method);
            req.setBody(body);
            Http http = new Http();
            HttpResponse res = http.send(req);
        }
 
    }
    
}
Test class 
@isTest
private class SlackOpportunityTest {
    

static testMethod void testpostToSlack() {
SlackOpportunityPush.Oppty opt = new SlackOpportunityPush.Oppty();
opt.opptyName = 'Unit Test Opt';
opt.Owner = 'Unit Test Owner';


List<SlackOpportunityPush.oppty> lis = new List<SlackOpportunityPush.oppty>();
lis.add(opt);
SlackOpportunityPush.postToSlack(lis);

System.assertEquals(lis, lis); 
    
}
     
}

Code coverage is now 100% but while deploying the classes I get error:

​Methods defined as TestMethod do not support Web service callouts 
Stack Trace: null
Best Answer chosen by Hormoz Hekmat 7
AnupamAnupam
Web service callouts are not supported in test classes.
Update your apex call to return a mock response in case it's been called from test class.

if (Test.isRunningTest()) {  
return mock response
}else{
HttpResponse res = http.send(req);
}

All Answers

AnupamAnupam
Web service callouts are not supported in test classes.
Update your apex call to return a mock response in case it's been called from test class.

if (Test.isRunningTest()) {  
return mock response
}else{
HttpResponse res = http.send(req);
}
This was selected as the best answer
Hormoz Hekmat 7Hormoz Hekmat 7
Hi Anupam,

       I have not understood it completely, Do you mean adding mock response to the original class ? and How ?
Hormoz Hekmat 7Hormoz Hekmat 7
Thanks , got it.
Won Young JungWon Young Jung

Hi Hormoz,

Which of the classes did you end up inserting the if else statement?