• Vishnu Menon
  • NEWBIE
  • 0 Points
  • Member since 2015
  • Senior Software Engineer
  • Heidelsoft Technologies

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
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
 
@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.

 
Hi All,

I am trying to dynamically set field values of an Sobject from a Visualforce page, then typecast it to Campaign object and then insert it to database.

However, I am unable to set the field values of the sobject through <inputField> tag. Below is the relevant parts of my code

VisualForce Page
 
<apex:outputpanel id="requiredFieldsBlock">
      <apex:pageBlock title="{!$Label.MandatoryFieldsPrompt}" rendered="{!isCampaignSelected}">
      <apex:pageBlockTable id="fieldsTable" title="Mandatoryfields" value="{!MandatoryFieldsWrapperList}" var="mandatoryFieldWrapper">

        <apex:column headerValue="{!$Label.MandatoryFields}" value="{!mandatoryFieldWrapper.fieldLabel}" style="width:500px; height: 28px; text-align:center"/> 

        <apex:column headerValue="{!$Label.Value}">
          <apex:inputField value="{!campaignToInsert[mandatoryFieldWrapper.fieldApi]}" required="true" style="width: 600px; height: 28px" />
        </apex:column>
      </apex:pageBlockTable>
      <apex:commandButton action="{!insertCampaignToDb}" styleClass="slds-button slds-button--brand slds-m-around--large" reRender="requiredFieldsBlock,hiddenBlock" onComplete="removeEmptyErrorMessage()" status="renderLightningSpinner" value="Create Campaign"/>
      <apex:commandButton html-novalidate="novalidate" immediate="true" action="{!back}" styleClass="slds-button slds-button--brand slds-m-around--large slds-float--right" reRender="errorMessageBlock,hiddenBlock" status="renderLightningSpinner" value="{!$Label.Back}"/>
         </apex:pageBlock> 
    </apex:outputpanel>

Controller
 
public with sharing class ImportCampaignsAngularPoCCntrl {
public Sobject campaignToInsert {get;set;}
public ImportCampaignsAngularPoCCntrl () {
campaignToInsert = Schema.getGlobalDescribe().get('Campaign').newSObject(); 
}

public PageReference insertCampaignToDb() { 
campaignToInsert = (Campaign) campaignToInsert;
insert campaignToInsert ;
}
public with sharing class MandatoryFieldsWrapper {
        public String fieldLabel {get;set;}
        public String fieldApi {get;set;}
        public Campaign importedcampaign {get;set;}
                
        public MandatoryFieldsWrapper (String fieldLabel, String fieldApi, Campaign importedcampaign) {
            this.fieldLabel = fieldLabel;
            this.fieldApi = fieldApi;
            this.importedcampaign = importedcampaign;        
        }    
    }


On debugging, I could see that campaignToInsert is not getting the fieldvalues entered via the VF page. Please help in correctly obtain the fieldvalues.