0

while calling method

@isTest
  public class call_Test{
     static testMethod void unitTest(){
      uptodel.qTis();
     }
  }

its given error that Methods defined as TestMethod do not support Web service callouts

public class uptodel {
     public static final String ts_url = 'https://example.com/study';
     public static void qTis() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(ts_url);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
            if (response.getStatusCode() == 200) {
                Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                List<Object> contacts = (List<Object>) results.get('contacts');
                for (Object contact: contacts) {
                    System.debug(contact);
                }
            }

    }
}
  • Have a look Mock Api callout for test class – Reshma Mar 2 '17 at 11:51
  • @cloudZigZag You need to create a Mock Test class for covering these kind of classes. Refer this link. – Rohit Mourya Mar 2 '17 at 11:51
  • my method is void type and in this example they return something how to implement this thing @RohitMourya – cloudZigZag Mar 2 '17 at 12:07
  • @cloudZigZag That was just an example. Try it and post your code if you get stuck. – Rohit Mourya Mar 2 '17 at 12:24
  • What does the method do? Does it update a List<Contact>? You could query for the Contacts and check they're updated, using the mock callout/response as @RohitMourya suggested. If you are updating Contacts, you can assert that the Contact list is updated based on the response or not. – Dan Jones Mar 2 '17 at 13:21
0

Since your test calls a method that executes a callout you need to mock the response during test methods.

The easiest way to do this is in sfdcfox's post here:

Help on Invokable Apex Test class and @Future callout Apex test Class

@isTest class MakeCalloutTest {
    // Simple echo callout class
    // Returns whatever response we tell it to when asked
    class EchoHttpMock implements HttpCalloutMock {
        HttpResponse res;
        EchoMock(HttpResponse r) {
            res = r;
        }
        // This is the HttpCalloutMock interface method
        public HttpResponse respond(HttpRequest req) {
            return res;
        }
    }

    @isTest static void test() {
        // Avoid using live data
        List<Lead> leads = new List<Lead>{ new Lead(LastName='Test',Company='test') };
        insert leads;
        // We tell it what to simulate
        HttpResponse res = new HttpResponse();
        res.setBody('<?xml version="1.0" encoding="utf-8"?><root U_Id="12345"></root>');
        res.setStatusCode(200);
        // This allows the callout to succeed
        Test.setMock(HttpCalloutMock.class, new EchoHttpMock(res));
        // Start the test
        Test.startTest();
        // Enqueue the future call
        MakeCallout.invokeleadcallout(leads);
        // Trigger future method
        Test.stopTest();
        // Verify logic
        leads = [select id__c from lead];
        System.assertEquals('12345', leads[0].Id__c);
    }
}

He was even kind enough to put an example test method in there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.