0

Recently working test classes in Salesforce started to Fail due to this error

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

I have reviewed the SF response https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm

but do not understand where to implement the call out in the test class.

I created a Mock Webserivce class but can not figure out where to call the mock class.

This is the test class that is failing:

@isTest
public class OppLineItemInventoryTriggerTest {
    static testmethod void testdemo() {
        Account act = new Account(lastName = 'Testing', Billingstreet = '1234 Main', billing_as_shipping__c = True, Business_Unit__c = 'Vaya');
        insert act;
        Product2 prod = TestUtils.createProduct('product1');
        Id RecordTypeIdopp = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Package Lot').getRecordTypeId();
        Opportunity opp = new Opportunity();
        opp.AccountId = act.ID;
        opp.Name = 'Test';
        opp.Status__c = 'Completed';
        opp.Transfer_Type__c = 'Delivery';
        opp.StageName = 'New';
        opp.recordtypeid = RecordTypeIdopp;
        opp.CloseDate = Date.newInstance(2016, 12, 9);
        insert opp;
        Dosage__c dos = new Dosage__c();
        dos.Product__c = prod.ID;
        dos.Unit__c = 'mg';
        dos.Value__c = 5;
        insert dos;
        Product_Lot__c prolo = new Product_Lot__c();
        insert prolo;
        String standardPricebookId = Test.getStandardPricebookId();
        PricebookEntry pdb = new PricebookEntry(Pricebook2Id = standardPricebookId, Product2Id = prod.Id, IsActive = true, UnitPrice = 100);
        insert pdb;
        Opportunity opps = [Select id from Opportunity Limit 1];
        Contact con = new Contact(LastName = 'TestName');
        insert con;
        OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opps.Id, PricebookEntryId = pdb.Id, Quantity = 10, Shipping_Datetime__c = NULL, TotalPrice = 3000);
        insert oli;
        Inventory__c inv = new Inventory__c();
        inv.Product_ID__c = prod.ID;
        inv.Status__c = 'Completed';
        inv.Opportunity__c = opp.ID;
        inv.Days__c = '30';
        inv.Prescription__c = 'www.google.com';
        inv.Prescription_Line_Item_Id__c = 'test';
        inv.Product_Lot__c = prolo.ID;
        inv.Purchase_Date__c = Date.newInstance(2016, 12, 9);
        insert inv;
        sObject[] sObjectOldList = new sObject[] {};
        sObject[] sObjectNewList = new sObject[] {};
        sObject s11,s12;
        //sObjectOldList.add(oli);
        //sObjectNewList.add(oli);
        OppLineItemInventoryTrigger s1 = new OppLineItemInventoryTrigger(sObjectOldList, sObjectNewList);
        //  s1.execute(sObjectOldList, true);
        s1.execute(sObjectNewList, true);
        s1.executable(s11, s12);
    }

I created this mock webservice class

global class YourHttpCalloutMockImpl implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) {
        HttpResponse res = new HTTPResponse();
        res.setHeader('Content-Type', 'application/JSON');
        res.setBody('Your body');
        res.setStatusCode(201);
        return res;
    }
}

But not sure how to call it in the test class.

David Reed
  • 2,522
  • 2
  • 16
  • 16
kpedrick1
  • 15
  • 1
  • 5

2 Answers2

1

You don't need to explicitly invoke your mock class within your unit test. Rather, you configure it with a Test.setMock() call like

Test.setMock(WebServiceMock.class, new YourHttpCalloutMockImpl ());

for a SOAP web service, or

Test.setMock(HttpCalloutMock.class, new YourHttpCalloutMockImpl ());

Make sure you perform this call before you invoke the code that you're intending to test, which makes a callout. The system will automatically use your mock class to respond to the outbound web service call - no further intervention on your part is required.

Right now, you seem to be looking at the SOAP documentation, but have written an HTTPCalloutMock - make sure that you're adopting the mock interface that matches the type of call your code makes.

David Reed
  • 2,522
  • 2
  • 16
  • 16
  • Thank you for your feedback David, I am a new Dev that is trying to update existing code. So the Test class that I have listed need to call the first line? Where in this example would I insert ``` Test.setMock(WebServiceMock.class, new YourHttpCalloutMockImpl ()); ``` – kpedrick1 Jul 09 '19 at 15:29
  • @kpedrick1 All it has to do is come before you actually call the code which ultimately makes the callout. – David Reed Jul 09 '19 at 16:33
  • When I add the line after line 2 or 3 I get a syntax error. Sorry Noob Question – kpedrick1 Jul 09 '19 at 17:19
0

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

This error occurs because web service callouts are not allowed in Test class.

To bypass callouts add HttpcalloutMock class.

Here is a sample test class with the mock class to bypass web services Callout.

********************** Test Class ***************************

@isTest 
private class Test_class {

    private class RestMock implements HttpCalloutMock {

        public HTTPResponse respond(HTTPRequest req) {
            String fullJson = 'your Json Response';

            HTTPResponse res = new HTTPResponse();
            res.setHeader('Content-Type', 'text/json');
            res.setBody(fullJson);
            res.setStatusCode(200);
            return res;
        }
    }
    static testMethod void service_call() {

        Test.setMock(HttpCalloutMock.class, new RestMock());
        Test.startTest();

        //your web service call code

        Test.StopTest();


    }
}

The mock class will take care of web services callouts.

Arjun
  • 769
  • 1
  • 5
  • 20