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
TechBlossomTechBlossom 

test class for batch apex making http apex callouts

Hi guys! I am a new developer, and I have trouble writig test class for the following batch class. Any kind of help is greatly appreciated. Thanks in advance. 

global class batchAccountsImport implements 
             Database.Batchable<SObject>,Database.AllowsCallouts {
  
  global Database.queryLocator 
                    start(Database.BatchableContext ctx){
        return Database.getQueryLocator([select id,safariUserId__c, isActive from User where isactive = true]);
    }
    global void execute(Database.BatchableContext ctx, List<Sobject> scope){
       String url = 'http://75.24.90.133:8080/restdemo/services/webservices';
       
       List<User> sfuser = (List<User>)scope; 
       List<Account> finalAccts = new List<Account>();
       system.debug('################################################################'+sfuser.size());
       
       for(integer i=0; i<sfuser.size(); i++)
       {
          Http http1 = new Http();
          System.debug('callout 1');
          HttpRequest req1 = new HttpRequest();
          //req1.setTimeout(120000); // timeout in milliseconds
          req1.setEndpoint(url+'/customers/'+sfuser[i].safariUserId__c);
          System.debug('################'+sfuser[i].safariUserId__c );
          req1.setMethod('GET');
          HttpResponse res1 = http1.send(req1);
          String xmlContent1= res1.getbody();
          Id userid = sfuser[i].id;
          List<Account> allAccts = xmlParser(xmlContent1,userid);
          for(integer j=0; j<allAccts.size();j++)
          {
             finalAccts.add(allAccts[j]);
          }
       }
       insert finalAccts;
    }
    
          
          public List<Account> xmlParser(String strXml1,Id currentUserid)
          {
          //Log the XML content
          System.debug('########Inside XMLParser Method########');
          Dom.Document doc1 = new Dom.Document();
          doc1.load(strXml1);
               
          List<Account> NewAccts = new List<Account> ();
        
          dom.XmlNode xroot1 = doc1.getrootelement();
          dom.XmlNode [] xrec1 = xroot1.getchildelements(); //Get all Record Elements
        
          for(Dom.XMLNode child1 : xrec1) //Loop Through Records
          {
             Account acct = new Account();
 
             for (dom.XmlNode awr1 : child1.getchildren() ) {
                    
                if (awr1.getname() == 'customerName') {
                   system.debug('#####################' + awr1.gettext());
                   acct.Name = awr1.gettext();
                }

                if (awr1.getname() == 'customerAccountType') {
                   system.debug('#####################' + awr1.gettext());
                   acct.Type = awr1.gettext();
                }  
      
                if (awr1.getname() == 'customerAddress') {
                   system.debug('#####################' + awr1.gettext());
                   acct.BillingStreet = awr1.gettext();
                }
                
                if (awr1.getname() == 'customerCity') {
                   system.debug('#####################' + awr1.gettext());
                   acct.BillingCity =awr1.gettext();
                }
                    
                if (awr1.getname() == 'customerZip') {
                   system.debug('#####################' + awr1.gettext());
                   acct.BillingPostalCode = awr1.gettext();
                } 
      
                if (awr1.getname() == 'customerState') {
                   system.debug('#####################' + awr1.gettext());
                   acct.BillingState = awr1.gettext();
                }
                
                if (awr1.getname() == 'customerCountry') {
                   system.debug('#####################' + awr1.gettext());
                   acct.BillingCountry = awr1.gettext();
                }
                
                if (awr1.getname() == 'customerId') {
                   system.debug('#####################' + awr1.gettext());
                   acct.safariAccId__c = awr1.gettext();
                }
                  
                if (awr1.getname() == 'userid') {
                   system.debug('#####################' + awr1.gettext());
                   acct.ownerId = currentUserid;
                }  
                
             }
             NewAccts.add(acct);
        
          }
          //insert NewAccts;
          return NewAccts;
      }
      global void finish(Database.BatchableContext ctx){
      
         //Send an email to the User after your batch completes  
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
         String[] toAddresses = new String[] {'samhita.ps@gmail.com'};  
         mail.setToAddresses(toAddresses);  
         mail.setSubject('Apex Batch Job is done');  
         mail.setPlainTextBody('The batch Apex job processed ');  
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
      }

 }

chrinderPWWchrinderPWW
You should be able to do this using HttpCalloutMock and Test.startTest/stopTest.  But as this post points out you currently aren't able to do this.  Hopefully future update will fix/allow this. http://salesforce.stackexchange.com/questions/11606/testing-batch-job-with-http-callouts