0

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.

TestClass

   @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;
}

TestMockClass

 @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 feature works fine without any errors outside test class.

Your Answer

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

Browse other questions tagged or ask your own question.