0

I need to cover some new functionality with tests, and for this I need to convert the lead in the test. I have two sandboxes (one is where we are developing; the second is for testing). In the first, everything is OK - the test passes and covers my new functionality. And on the second - an error, the test does not pass:

Methods defined as TestMethod do not support Web service callouts.

I have done a lot of manipulations in order to try to figure out where the callout occurs, but to no avail. Please advise me how I can identify the callout?

method for covering

public void fillingAddressForCustomer(List<Lead> newLeads, List<Lead> oldLeads) {
    Set<Id> accountsId = new Set<Id>();
    for(Integer i = 0, s = Trigger.new.size(); i < s; i++) {
        if(!oldLeads[i].isConverted && newLeads[i].isConverted) {
            accountsId.add(newLeads[i].convertedAccountId);
        }
    } if(accountsId.size() > 0) {
        Map<Id, Account> mapAccounts = new Map<Id,Account>([SELECT BillingAddress, BillingCity, BillingCountry, BillingPostalCode, BillingState, BillingStreet, ShippingAddress
                                                            FROM Account
                                                            WHERE ID =:accountsId]);
        List<Account> accountsForUpdate = new List<Account>();
        for (Lead l : newLeads) {
            Account tempAcc = mapAccounts.get(l.convertedAccountId);
            if((l.City != null 
                || l.Country != null
                || l.PostalCode != null
                || l.State != null
                || l.Street != null)
                && tempAcc.BillingAddress != null
                && (tempAcc.BillingCity != l.City
                || tempAcc.BillingCountry != l.Country
                || tempAcc.BillingPostalCode != l.PostalCode
                || tempAcc.BillingState != l.State
                || tempAcc.BillingStreet != l.Street)) {
                tempAcc.ShippingCity = l.City; 
                tempAcc.ShippingCountry = l.Country;
                tempAcc.ShippingPostalCode = l.PostalCode;
                tempAcc.ShippingState = l.State;
                tempAcc.ShippingStreet = l.Street;
                accountsForUpdate.add(tempAcc);
            }
        }
        if (accountsForUpdate.size() > 0) {
            update accountsForUpdate;
        }
    }
}

test method

@isTest private static void testFillingAddressForCustomer() {
    
    Account testAccount = TestFactory.createAccount('Test Account');
    testAccount.Was_Lead__c = false;
    testAccount.BillingCountry = 'Country';
    testAccount.BillingCity = 'City';
    testAccount.BillingPostalCode = 'Postal Code';
    testAccount.BillingState = 'State';
    testAccount.BillingStreet = 'Street';
    insert testAccount;

    Lead testLead = new Lead(Email = '[email protected]',
                             LastName = 'Test',
                             Country = 'Lead Country',
                             City = 'Lead City',
                             PostalCode = 'Lead Postal Code',
                             State = 'Lead State',
                             Street = 'Lead Street');
    insert testLead;

    test.startTest();
    Database.LeadConvert lc = new Database.LeadConvert();
    lc.setDoNotCreateOpportunity(true);
    lc.setAccountId(testAccount.Id);
    lc.setLeadId(testLead.id);
    lc.setConvertedStatus('Closed - Converted');

    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    lc.setConvertedStatus(convertStatus.MasterLabel);

    Database.LeadConvertResult lcr = Database.convertLead(lc);
    System.assert(lcr.isSuccess());
    test.stopTest();

    testAccount = [SELECT ShippingAddress FROM ACCOUNT WHERE ID =: lcr.getAccountId() LIMIT 1];
    System.assertNotEquals(testAccount.ShippingAddress, null);
}

enter image description here

1

2 Answers 2

0

Run your test from the Developer Console (in Salesforce), and you'll get a log file in the Logs tab. Open that, and you should see exactly which line the exception is thrown from.

Basic Example

@isTest class q363955 {
    @isTest static void test() {
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint('https://www.google.com/');
        new Http().send(req);
    }
}

You'll then see the log:

Log Tab

And the log will look something like this:

53.0 APEX_CODE,DEBUG;APEX_PROFILING,FINEST;CALLOUT,NONE;DB,FINEST;NBA,NONE;SYSTEM,FINE;VALIDATION,NONE;VISUALFORCE,NONE;WAVE,NONE;WORKFLOW,FINER
06:48:38.2 (2338783)|EXECUTION_STARTED
06:48:38.2 (2450091)|CODE_UNIT_STARTED|[EXTERNAL]|01p1T0000042Spn|q363955.test()
06:48:38.2 (3448028)|SYSTEM_METHOD_ENTRY|[4]|System.HttpRequest.setMethod(String)
06:48:38.2 (3702219)|SYSTEM_METHOD_EXIT|[4]|System.HttpRequest.setMethod(String)
06:48:38.2 (3944800)|SYSTEM_METHOD_ENTRY|[5]|System.HttpRequest.setEndpoint(String)
06:48:38.2 (4022049)|SYSTEM_METHOD_EXIT|[5]|System.HttpRequest.setEndpoint(String)
06:48:38.2 (4088844)|SYSTEM_METHOD_ENTRY|[6]|System.Http.send(ANY)
06:48:38.2 (4916328)|EXCEPTION_THROWN|[6]|System.TypeException: Methods defined as TestMethod do not support Web service callouts
06:48:38.2 (5036360)|SYSTEM_METHOD_EXIT|[6]|System.Http.send(ANY)
06:48:38.2 (5240564)|FATAL_ERROR|System.TypeException: Methods defined as TestMethod do not support Web service callouts

Class.q363955.test: line 6, column 1
06:48:38.2 (5417421)|FATAL_ERROR|System.TypeException: Methods defined as TestMethod do not support Web service callouts

Where it says Class.q363955.test: line 6, column 1 in my example shows that it crashed on line 6 with this error. You can read this information to determine the path (the list will include a full stack trace), so you can easily identify the problem path.

The solution to this problem is to add a callout mock, as described in the documentation.

4
  • Yes, thank you, I looked at the log file and here it is (added a screenshot), but I didn't understand what I need to fix based on this... Commented Dec 10, 2021 at 14:02
  • I'm not sure, but it seems to me that mock won't help. The most important thing I need to set for my Lead record is "isConverted" to "true" and fill in "convertedAccountId". But these fields cannot be changed and I will not be able to create a record based on the JSON received from mock. Or am I wrong? Commented Dec 10, 2021 at 14:42
  • @uniqueuser sounds like you have a trigger with asynchronous code that makes a callout. Try using a callout mock and you should be able to identity the source.
    – sfdcfox
    Commented Dec 10, 2021 at 15:13
  • Yes, logically it happens somewhere. I have a lot of logic for different objects trigger calls. And the code was written before me. But so far the search does not give anything. I will continue. Commented Dec 10, 2021 at 15:24
0

The two fields isConverted and convertedAccountId are set by Lead Conversion. In Apex, that is be done with the method Database.convertLead . As you observed, your code cannot set the fields; but convertLead() can .
You code you are testing probably has that call, but execution isn't getting to that point because the forbidden-in-testing callout occurs first. You need a mock callout, as sfdcfox posted. The code you are testing must avoid doing the real callout during tests. It should be coded with if (! Test.isRunningTest()) . To find the right place to make that change, use the Developer Console, as sfdcfox posted.

You must log in to answer this question.

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