You need to sign in to do that
Don't have an account?
msaz87
Methods defined as TestMethod do not support Web service callouts (can't find a solution that works)
I'm receiving the following error during validation/deployment: "Methods defined as TestMethod do not support Web service callouts"
My trigger doesn't contain a callout, but from what I've read it's likely being triggered by a package or another trigger.
I've seen other posts on this subject, and I've tried all their solutions including mocking HTTP responses and callouts, but nothing seems to work. Can anyone help?
Below are the trigger and two classes being deployed:
Trigger
Class referenced in trigger
My trigger doesn't contain a callout, but from what I've read it's likely being triggered by a package or another trigger.
I've seen other posts on this subject, and I've tried all their solutions including mocking HTTP responses and callouts, but nothing seems to work. Can anyone help?
Below are the trigger and two classes being deployed:
Trigger
trigger updateContactAfterConverted on Lead (after update) { for(Lead lead:System.Trigger.new) { // was the lead converted? if (Lead.IsConverted) { // query new contact Contact newContact = [SELECT Id FROM Contact WHERE Contact.Id = :lead.ConvertedContactId]; // run @future class to update contact after conversion completed updateContactAfterConvertedFuture.myMethod(newContact.id); } } }
Class referenced in trigger
public class updateContactAfterConvertedFuture { @future public static void myMethod(String newContact) { // Find new contact Contact updateContact = [SELECT Id FROM Contact WHERE Contact.Id = :newContact LIMIT 1]; // Set field to true updateContact.Conversion_Completed__c = TRUE; // Update contact update updateContact; } }Test class
@isTest private class testUpdateContactAfterConverted { static testMethod void myUnitTest() { // Create new test lead Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons', LeadSource = 'Advertising', Lead_Source_Detail__c = 'PPC'); insert myLead; // Convert test lead Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(myLead.id); // Check conversion LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1]; lc.setConvertedStatus(convertStatus.MasterLabel); // Declare successful Database.LeadConvertResult lcr = Database.convertLead(lc); System.assert(lcr.isSuccess()); } }I also tried not including the test class in the deployment, but without it I get 0% code coverage on the trigger.
Hi find this
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
Create a Mockresponse generator class and change your test class like what I did
Stack Trace: Class.testUpdateContactAfterConverted.myUnitTest: line 26, column 1"
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
if(lcr.isSuccess())
{
Contact newCOntact = [Select If FROM Contact where Id =:myLead.ConverttedContactId];
updateContactAfterConvertedFuture.myMethod(newContact.id);
}
Test.StopTest();
one line 29 just query for contact
Contact newContact = [Select Id from Contact LIMIT 1];
Sorry I thought that is httpcallout
Please refer this https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
Here's the updated test: And per your link I added three more classes:
docsample and WebSvcCallout are sample classes they gave that class just for understanding, our class is updateContactAfterConvertedFuture.....
so please remove those 2 classes and use updatecontactafterconverted class in WebServiceMockImpl
- WebSvcCallout: Invalid type: docSample.DocSamplePort
- WebServiceMockImpl: Invalid type: docSample.EchoStringResponse_element
- testUpdateContactAfterConvertedOrig: Dependent class is invalid and needs recompilation: WebServiceMockImpl: line 14, column 16: Invalid type: docSample.EchoStringResponse_element
And here are the classes:WebSvcCallout WebServiceMockImpl testUpdateContactAfterConvertedOrig
Getting rid of doc sample means we need to remove refereces in other classes as well just take out docsample class and put your class.
If I updated the two classes they would look like this:
WebSvcCallout WebServiceMockImpl
I don't know in line 3 why oyu kept DocSamplePort() ?? you have to put your future method there.
are you new to coding ?
So is this correct?
WebSvcCallout WebServiceMockImpl
remove 13 to 17 in WebServiceMockImpl
Greetings!
The method which is testmethod, does not allow web callout.
So it means test classes are not allowed to web callout.
So its solution is to create a fake response.
To creating a fake response, use HttpCalloutMock interface.
If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi