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
balaji raobalaji rao 

write a test class for rest api callout ?

write a test class for rest api call out 
Amit Shingavi 8Amit Shingavi 8
Hi Balaji,

To write test classes for Rest API callouts, you have to use HttpCalloutMock interface to create a mock response.
Follow this link (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm)to see the examples.

Do post here if you still have any queries.

Happy Coding! 

Regards,
Amit Shingavi
 
Raj VakatiRaj Vakati
YOu need to  Provide an implementation for the HttpCalloutMock interface to specify the response sent in the respond method, which the Apex runtime calls to send a response for a callout.
 
@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.
        System.assertEquals('http://example.com/example/test', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"example":"test"}');
        res.setStatusCode(200);
        return res;
    }
}
 
@isTest
private class CalloutClassTest {
     @isTest static void testCallout() {
        // Set mock callout class 
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        
        // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock. 
        HttpResponse res = CalloutClass.getInfoFromExternalService();
        
        // Verify response received contains fake values
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"example":"test"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
    }
}

 
balaji raobalaji rao
Thank you to all,
still iam facing the issue.

my scenario is first we have to insert the record in target ,we have to store the external id of that record ,using that id have to update the same record 
August hhgfvvbAugust hhgfvvb
The simplest request is a GET request (GET is an HTTP method). A GET request means that the sender wants to obtain information about a resource from the server. When the server receives and processes this request, it returns the request information to the recipient. A GET request is similar to navigating to an address in the browser https://vivavideomaker.com/
deen markdeen mark
Nice information and thanks for sharing this to us. Get more about the best apps here. https://tcswebmail.vip
fele ramfele ram
Very interesting, I can get some useful tips that I didn’t know before. You made my day awesome, Thanks. comparatif imprimante multifonction (https://permataindahtrans.com/meilleur-imprimante-multifonction/)
apk forpcapk forpc
really great post admin thanks for sharing this post.
https://www.foxtechzone.com/2020/04/kodi-apk-for-android-tv-pc-download.html
apk forpcapk forpc
Great post admin thanks for sharing this really worthy.
https://www.jiotvforpcwindowsdownload.in/2019/11/jiotv-web-version-free.html
tech advisortech advisor
Find all the free software downloads, hardware reviews, online tools, web apps, and tech news.
https://www.techappszone.com/2020/01/viva-video-for-pc-windows.html
tech advisortech advisor
Thanks for sharing this information.
https://www.zaixtech.com/helo-app-for-pc-windows/
Stuart WingetStuart Winget
If you looking forward to chat with unknown peoples online the do check out Omegle tv video chat app - https://chatmates.net/omegle-tv-online/
play stationplay station
Don't worry about the way to process the entire system of the entertainment source by the ultimate.
https://techbaleno.com/windows/playstation-messages-pc/
Joice ClaraJoice Clara
If you want to remove brace on your photoshot you can check best apps to remove brace on https://www.venostech.com/best-remove-braces-apps/
cris millcris mill
Halki Diabetes Remedy is every Type 2 Diabetes' suffers' guide to restoring and regaining their health, safely and naturally. Halki Diabetes Remedy Reviews greatly helps to decrease the chances of type 2 diabetics by eliminating toxins in a perfect way. https://halkidiabetesremedy.guru
large donlarge don
SpyHunter is safe to use and very effective against any sorts of threat trying to invade your system. But, it is not completely safe for real-time protection as it doesn't work in real-time. You have to manually scan the computer to detect and remove the malware. Get is spyhunter safe details here.https://kinetichifi.com/is-spyhunter-safe/
Suraj Tripathi 47Suraj Tripathi 47

Hi balaji,

To write test classes for Rest API callouts, you need to use the HttpCalloutMock interface to generate a mock or fake response.

for more information go through this link:
https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_rest_callouts

If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi
Jagjit Singh 75Jagjit Singh 75

Thanks for sharing this information with us. It will really helpful for us. For latest apk for JioTV go to Telecom Guruji (https://www.telecomguruji.com" target="_blank).