+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

Facing following error - Methods defined as TestMethod do not support Web service callouts

Hi,

I am facing following Error while run a test class. 

Error : 
Methods defined as TestMethod do not support Web service callouts


Test Class : 
 
@isTest
public class AccountProcessorTest {
    
    @isTest
    public static void testFunc() {
        account acc = new account();
        acc.name = 'MATW INC';
        insert acc;
        
        contact con = new contact();
        con.lastname = 'Mann1';
        con.AccountId = acc.Id;
        con.email = '123@gmail.com';
        insert con;
        contact con1 = new contact();
        con1.lastname = 'Mann2';
        con1.AccountId = acc.Id;
        con.email = '124@gmail.com';
        insert con1;
        
        
        List<Id> acc_list = new List<Id>();
        acc_list.add(acc.Id);
        Test.startTest();
            AccountProcessor.countContacts(acc_list);
        Test.stopTest();
        List<account> acc1 = new List<account>([select Number_of_Contacts__c from account where id = :acc.id]);
        system.assertEquals(2,acc1[0].Number_of_Contacts__c);
    }

}

Note : Facing this error while cover a test class for @future Method 
 
public class AccountProcessor {
    
    @future
    public static void countContacts(List<Id> accountId_lst) {
        
        Map<Id,Integer> account_cno = new Map<Id,Integer>(); 
        List<account> account_lst_all = new List<account>([select id, (select id from contacts) from account]);
        for(account a:account_lst_all) {
            account_cno.put(a.id,a.contacts.size()); //populate the map
            
        }
                    
        List<account> account_lst = new List<account>(); // list of account that we will upsert
            
        for(Id accountId : accountId_lst) {
            if(account_cno.containsKey(accountId)) {
                account acc = new account();
                acc.Id = accountId;
                acc.Number_of_Contacts__c = account_cno.get(accountId);
                account_lst.add(acc);
            }
            
        }
        upsert account_lst;
    }

}


Regards,
Soundar.
Best Answer chosen by Soundar Raj Ponpandi
Raj VakatiRaj Vakati
The error is not coming from this class .. looks like you have an apex trigger that is calling the web serverice and you are getting this error message from the trigger into the test class 


you may want to include Test.isRunningtest()in your actual callout class to check you are not going to run actual callout and only test mock and You will have to use callout mock intefaces to test classes making http calllout.



https://salesforce.stackexchange.com/questions/3486/testing-httpcallout-with-httpcalloutmock-and-unittest-created-data
https://inteygrate.com/test-class-for-apex-methods-making-callouts/