Test apex rest class Callouts using StaticResourceCalloutMock


Everybody googles about covering the rest callouts and ends up using almost one solution, i.e., creating a Mockup service class and use it in the test class. But there is an easy way and its been out there for quite sometime. It is really so simple that you can end up writing the test class in less than a minute, you need to have the response JSON of the callout that you are making in your actual class. You will have to catch that from either system.debug(res.getBody()) or get it from the other system.
That would look something like this:
{"status":"OK","message":"Search request completed successfully","result":{"appId":110,"requestId":225877959,"resultCount":9,"pageInfo":{"pageSize":10,"requestedPage":2,"totalPages":2,"totalResults":19},"nextPage":false,"sourceItemIds":"1101-1110-1111"}}
1. Catch that response in a text file.
2. Create a static resource and upload this raw text file. EX: sr_jsonresponse
3. Add the following code to the test class before you make the callout, like calling the method or making DML,
        StaticResourceCalloutMock MyMockingJay = new StaticResourceCalloutMock();
        
MyMockingJay.setStaticResource('sr_jsonresponse');
        
MyMockingJay.setStatusCode(200);
        Test.setMock(HttpCalloutMock.class, 
MyMockingJay);
There you go, you can now add upsert statements or calling the call-out methods. Doing this makes the mock system for a callout.

Isnt simple than creating the mockservice class?!?

HTH
Prabhan
       

Comments

  1. Test Apex Rest Class Callouts Using Staticresourcecalloutmock >>>>> Download Now

    >>>>> Download Full

    Test Apex Rest Class Callouts Using Staticresourcecalloutmock >>>>> Download LINK

    >>>>> Download Now

    Test Apex Rest Class Callouts Using Staticresourcecalloutmock >>>>> Download Full

    >>>>> Download LINK 9w

    ReplyDelete

Post a Comment

Popular posts from this blog

Lightning spinner inside Button

Passing URL parameters to controller in Lightning components/ AURA

Nested AURA:IFs in Lightning Components Salesforce