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
Daniel B ProbertDaniel B Probert 

httpcallout test help

hi all anyone able to help me with a test class that i'm working on.

 

I have a trigger on an object that when a new record is created it calls an external website and imports an xml file(this works)

 

trigger:

trigger MonitoringUpdater on Monitoring_Update__c (after insert) {

  for (Monitoring_Update__c mformupdate : Trigger.New) {
    MonitoringFormsUpdaterManual.updateMF();
  }

}

 the apex is:

 

public class MonitoringFormsUpdaterManual {
  @Future(callout=true)
  public static void updateMF() {
    Http http = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://magpi.com/api/surveys?username=username&accesstoken=authtoken');
    req.setMethod('POST'); 
    HTTPResponse res = http.send(req);
    System.debug(res.getBody());
    List<Monitoring_Form__c> newforms = new List<Monitoring_Form__c> ();
    set<string> existingmFormSetId = new set<string>();
 //iterate thru the existing mforms and add allthe form_id__c in a set. And then use this set to later check for duplicates
 for(monitoring_form__c mform: [SELECT Id, Form_id__c FROM Monitoring_Form__c]){
    existingmFormSetId.add(mform.Form_id__c);
 }
    string s = res.getBody();
    Dom.Document docx = new Dom.Document();
        docx.load(s);
        dom.XmlNode xroot = docx.getrootelement();
        dom.XmlNode [] xrec = xroot.getchildelements(); //Get all Record Elements
        for(Dom.XMLNode child : xrec) //Loop Through Records
            {
            Monitoring_Form__c mf = new Monitoring_Form__c();
            for(dom.XmlNode magpi : child.getchildren() ) {
               if (magpi.getname() == 'SurveyId') {
                     mf.form_id__c = magpi.gettext();
                 }
               if (magpi.getname() == 'SurveyName') {
                       mf.name = magpi.gettext();
                }
            }
            if(!existingmFormSetId.contains(mf.form_id__c)){
            newforms.add(mf);}
        }
        upsert newforms;
  }
        
  }

 now my test class that currently on has 24% coverage on the class and 100% on the trigger looks like this:

 

@isTest
global class Test_MFUpdateManualClass {
    public void myMonitoringManTest() {

// create a contact
Monitoring_Update__c monup = new Monitoring_Update__c();    
    monup.reason__c = 'test to fire of trigger';
insert monup;
Monitoring_Form__c mf = new Monitoring_Form__c();
    mf.form_id__c = '123';
    mf.name = 'Demo survey';
insert mf;
}
global class MockMagpiGenerator 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('https://magpi.com/api/surveys', req.getEndpoint());
        System.assertEquals('POST', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/xml');
        res.setBody('<Surveys><Survey><SurveyID>123</SurveyID><SurveyName>Demo survey</SurveyName></Survey></Surveys>');
        res.setStatusCode(200);
        return res;
    }
}}

 can anyone guide me in the right direction i know that i have testing the callout it's the reading of the data part so this bit that I haven't tested:

 

List<Monitoring_Form__c> newforms = new List<Monitoring_Form__c> ();
    set<string> existingmFormSetId = new set<string>();
 //iterate thru the existing mforms and add allthe form_id__c in a set. And then use this set to later check for duplicates
 for(monitoring_form__c mform: [SELECT Id, Form_id__c FROM Monitoring_Form__c]){
    existingmFormSetId.add(mform.Form_id__c);
 }
    string s = res.getBody();
    Dom.Document docx = new Dom.Document();
        docx.load(s);
        dom.XmlNode xroot = docx.getrootelement();
        dom.XmlNode [] xrec = xroot.getchildelements(); //Get all Record Elements
        for(Dom.XMLNode child : xrec) //Loop Through Records
            {
            Monitoring_Form__c mf = new Monitoring_Form__c();
            for(dom.XmlNode magpi : child.getchildren() ) {
               if (magpi.getname() == 'SurveyId') {
                     mf.form_id__c = magpi.gettext();
                 }
               if (magpi.getname() == 'SurveyName') {
                       mf.name = magpi.gettext();
                }
            }
            if(!existingmFormSetId.contains(mf.form_id__c)){
            newforms.add(mf);}
        }
        upsert newforms;
  }
        

 

any guidance appreciated.

 

dan

 

 

Abhi_TripathiAbhi_Tripathi

Hey Camfed,

 

Everything looks fine as you are using mock class and everything which is to be done in test class, but am not confirm about this thing that in trigger you are calling a method but not passing records, how this thing works i can't understand .

 

So i think you should pass something through the method you are using , because it will call the method whenever triggers condition satisfies but as now its not.

 

 

Daniel B ProbertDaniel B Probert

Hi thanks for the quick response,

 

yes i believe from the testing of the callout I've got everything right as that's all tested what isn't being tested is the parsing of the data the comes from the callout.

 

the process is this.

 

1. I have an update on my external system

2. I create a new monitoring_update record - this invokes the trigger and class to go off to the external service and pull in all new records

 

So i can tell that I have tested the external call out but the parsing of the data retreived i can't work out how to test or include in my test.

 

does that help? there are no id's passed from the trigger as there is nothing to pass the monitoring_update object is just a way of triggering the import from the webservice.

 

let me know if this still doesn't make sense.

 

thanks

dan