function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PronowPronow 

Problem with Test Method which is using HTTP methods

Hello there,

 

I have a test method that is using following code:

 

01.    protected virtual HttpResponse executeLinkedInRequest(HttpRequest req)
02.    {
03.        HttpResponse res;
04.        res = new Http().send(req);
05.            
06.            if (req.getMethod() == 'POST' && res.getStatusCode() != 201)
07.            {
08.                System.debug('OAuth header:'+res.getHeader('oauth_problem'));
09.                throw new TwitterApiException (res.getBody());
10.            }
11.            else if (req.getMethod() == 'GET' && res.getStatusCode() != 200)
12.                throw new TwitterApiException (res.getBody());
13.        return res;
14.    }

 

When I am running the Test Class which contains this code, it is running till line number 04. Rest of the code comes in Red lines i.e. they are not getting executed. I know the root of the problem is HTTP().send(req) method. But, I am not able to understand why is that causing problem and how to make it work. Please help.

 

Thanks in advance.

swatKatswatKat

Isnt your test class failing ? We are not allowed to make callouts from a test method. 

 

Look at this if you want to cover classes with callouts.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm

PronowPronow

Well, My test class is not failing even though the execution is not going beyond that code. That is why I am not able to track the problem.

swatKatswatKat

Have u checked the debug log ? You will be able to find the execution summary there. That might help

PronowPronow

Actually my test class doesn't contain this code. I am just calling this code from my test class. I checked debug logs and its not giving any error or exception.

crop1645crop1645

@swatkat is correct -- testmethods will not execute callouts (nor will they send emails for that matter FYI). You need to use the Test.setMock() method to simulate the callout and its response.

 

Think about it -- if testmethods could execute callouts, those callouts could do destructive changes in some system - and not be rolled back like all other SFDC DML is rolled back upon conclusion of the testmethod 

 

Refer to the APEX dev Guide documentation as noted by @swatkat

PronowPronow
Thank you for your time and reply guys.