You need to sign in to do that
Don't have an account?
Vishnu Menon
Callout in test method not returning mock response
Hi,
I am trying to write a test class for testing an HTTP callout. Test class compiles without errors. However, I am not getting the mock response inside. Below is the relevant parts of my code.
Test Class
I am getting debug till RequestToSend . Not the Response. Cannot get the debug statement inside the respond method of TestMockingCrCampaigns. The code works fine without any issues outside test class.
I am trying to write a test class for testing an HTTP callout. Test class compiles without errors. However, I am not getting the mock response inside. Below is the relevant parts of my code.
Test Class
@isTest public static void testCRCampaignsCalloutSuccess() { CRAccount__c testCrAccount = (CRAccount__c)SmartFactory.createSObject('CRAccount__c',true); testCrAccount.namedCredential__c ='TestNamedCredential'; insert testCrAccount; Campaign testCampaign = (Campaign)SmartFactory.createSObject('Campaign',true); testCampaign.crId__c = '123456'; insert testCampaign; Test.setMock(HttpCalloutMock.class, new TestMockingCrCampaigns()); List<DO_Campaign> crCampaignsList = ImportCampaignsAngularPoCCntrl.getCrCampaigns(testCrAccount.namedCredential__c); system.assert(!crCampaignsList.isEmpty()); }Controller Method
public static List<DO_Campaign> getCRCampaigns(String namedcredential) { HttpRequest sfRequest = new HttpRequest(); HttpRequest req = new HttpRequest(); req.setEndpoint('http://api.salesforce.com/foo/bar'); req.setMethod('GET'); Http h = new Http(); system.debug('***RequestToSend***'+req); HttpResponse res = h.send(req); system.debug('***Response***'+response); List<DtoManager.CampaignDto> receivedCampaignDto = (List<DtoManager.CampaignDto>) JSON.deserialize(res.getbody(), List<DtoManager.CampaignDto>.class); if(receivedCampaignDto != null && !receivedCampaignDto.isEmpty()){ List<DO_Campaign> campaignsReceived = doManager.generateCampaigns(receivedCampaignDto); return campaignsReceived; }MockResponseClass
@isTest global class TestMockingCrCampaigns implements HttpCalloutMock { global HTTPResponse respond(HTTPRequest req) { system.debug('**Inside TestMockingCrCampaigns**'); HttpResponse res = new HttpResponse(); res.setHeader('Content-Type', 'application/json'); res.setBody('[{"id":"502097","name": "CreatingCampaignfromCR","stamp": 1491545738,"last_mailing": 1494914539,"last_changed": 1491545814,"is_locked": false},{"id": "502099","name": "CreatingGroupviaAPIPostman","stamp": 1491483714,"last_mailing": 0,"last_changed": 0,"is_locked": false}]'); res.setStatusCode(200); return res; } }
I am getting debug till RequestToSend . Not the Response. Cannot get the debug statement inside the respond method of TestMockingCrCampaigns. The code works fine without any issues outside test class.
Have you tried with Test.startTest() and Test.stopTest() ?
Thanks
Gulshan Raj
From your question I understand that your code compiles fine but test case assertion fails.
After analyzing your code, below are my finidngs:
1. List<DtoManager.CampaignDto> receivedCampaignDto = (List<DtoManager.CampaignDto>) JSON.deserialize(res.getbody(), List<DtoManager.CampaignDto>.class);
Here, 'receivedCampaignDto' cannot be empty or null as HttpResponse body in class 'TestMockingCrCampaigns' has been set with 2 json string objects (empty objects are created even after no attribute matches between json string object and field/property of corressponding class 'DtoManager.CampaignDto').
2. List<DO_Campaign> campaignsReceived = doManager.generateCampaigns(receivedCampaignDto);
I see the problem in this statement. Method 'doManager.generateCampaigns(receivedCampaignDto)' seems to return empty. Please check it.