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
RSKRSK 

How to write a test method for apex callout class

Hi, can any one help me how to write a test method for the Apex callout ..

Here is the class:

global public with sharing class HttpCallout_CLS{
 public static String hitNumbertemp,dunstemp,compnametemp,compIdtemp,citytemp,countrytemp;
    String Accid = apexPages.currentPage().getParameters().get('id');
 
 Public static void sendRequest(){
    Integer i=0,j,k;
    String env;
    HttpRequest req = new HttpRequest();
    req.setMethod('GET');
    req.setHeader('content-type','text/xml');
    req.setTimeout(12000);    
    req.setEndpoint('http://xxx.com/API-32');
    String API_KEY = 'xxxx';
    Blob headerValue = Blob.valueOf(API_KEY);
    String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
    req.setHeader('Authorization', authorizationHeader);
    String CName = apexPages.currentPage().getParameters().get('Name');
    env = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://applications.dnb.com/webservice/schema/">'+
            '<soapenv:Header>'+
              '<sch:API-KEY>xxx</sch:API-KEY>'+
            '</soapenv:Header>'+
            '<soapenv:Body>'+
              '<sch:FindCompanyByKeywordRequest>'+
              '<sch:maxRecords>100</sch:maxRecords>'+
              '<sch:sortDirection>Ascending</sch:sortDirection>'+
              '<sch:keyword>'+CName+'</sch:keyword>'+
              '<sch:searchBy>companyName</sch:searchBy>'+
              '<sch:returnSearchNavigation>true</sch:returnSearchNavigation>'+
              '<sch:orderBy>CompanyName</sch:orderBy>'+
               '</sch:FindCompanyByKeywordRequest>'+
              '</soapenv:Body>'+
             '</soapenv:Envelope>';      
    req.setBody(env);
    System.debug(req.getBody());     
    Http http = new Http();
    HTTPResponse res = http.send(req);
              // *******Parsing XML Response *******
        Dom.Document doc = res.getBodyDocument();
    Dom.XMLNode envelope = doc.getRootElement();
    Dom.XMLNode body = envelope.getChildElement('Body','http://schemas.xmlsoap.org/soap/envelope/');
    Dom.XMLNOde keyWordResponse = body.getChildElement('FindCompanyByKeywordResponse','http://applications.dnb.com/webservice/schema/');
     for(Dom.XMLNode h1 : keyWordResponse.getChildElements()){  
          for(Dom.XMLNode child : h1.getChildElements()){      
               for(Dom.XMLNode h2 : child.getChildElements()){ 
                    if(h2.getName() == 'hit'){
                        for(Dom.XMLNode h3 : h2.getChildElements()){   
                           if(h3.getName() != 'companyResults'){
                                  hitNumbertemp = h3.getText();
                                }
                          else{                                           
                              for(Dom.XMLNode  h4 : h3.getChildElements()){
                                 if(i==0 || i==1 || i==2 || i==4 || i== 5 )
                                       {
                                           if(i == 0)
                                           {
                                             compIdtemp = h4.getText();
                                           }
                                           if(i == 1)
                                           {
                                              dunstemp = h4.getText();
                                           }
                                           if(i == 2)
                                           {
                                               compnametemp = h4.getText();    
                                           }
                                           if(i == 4)
                                           {
                                               streettemp = h4.getText();
                                           }
                                           if(i == 5)
                                           {
                                               citytemp = h4.getText();
                                           }
                                                                                     
                                           i++;
                                        }
                                       else if(i==15){
                                          WrapperClassList wrap = New WrapperClassList();
                                           wrap.duNs = dunstemp;
                                           wrap.comPanyName = compnametemp;
                                           wrap.companyId = compIdtemp;
                                           wrap.street = streettemp;
                                           wrap.City = citytemp;
                                           listWrapper.add(wrap);
                                          }
                                           i = 0;
                                       }  
                                       else
                                       {
                                           i++;
                                       }
                                   }
                           }
                        }      
                    }   
                }
                   
       }               
     }
}
  public List<WrapperClassList> getdataList(){
     return listWrapper;
  }
 
  public static List<WrapperClassList> listWrapper = New List<WrapperClassList>();
  public static Map<String,WrapperClassList> mapListWrapper = New Map<String,WrapperClassList>();
  public Class WrapperclassList{
      public String companyId{set;get;}
      public String comPanyName{set;get;}
      public String duNs{set;get;}
      public String street{set;get;}
      public String City{set;get;}
    
  }
 
}

 Thanks a ton in advance!

 

- Kumar

Chamil MadusankaChamil Madusanka

Test class cannot be used to test Web service callout. The Test.isRunningTest() method is a useful method to bypass web service callouts when running automated test cases that would otherwise fail.

For example, in any wsdl2apex generated classes find the WebServiceCallout.invoke() method calls and then wrap them in an test for Test.isRunningTest(). If the test is false the code can still call invoke as generated. Otherwise the code can simulate the web service response, allowing the test cases to complete.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.