+ Start a Discussion
himanshu Soni 31himanshu Soni 31 

how to write a test class of callout external api in this code?

public class corona {

public List<coronawrap> ConsoleWrapperList{get;set;}
public List<coronawrap> ConsoleWrapperList2{get;set;}
    
 public String str{get; set;}
 public String str2{get; set;}
 

public List<coronawrap> getperformcallout(){

ConsoleWrapperList = new List<coronawrap>();

  

HttpRequest req = new HttpRequest();

HttpResponse res = new HttpResponse();

Http http = new Http();
    

req.setEndpoint('https://api.covid19api.com/countries');

req.setMethod('GET');

res = http.send(req);

if(res.getstatusCode() == 200 && res.getbody() != null){

ConsoleWrapperList=(List<coronawrap>)json.deserialize(res.getbody(),List<coronawrap>.class);

}
    

return consolewrapperlist;

}
public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        for(coronawrap crRec : ConsoleWrapperList){
            options.add(new SelectOption(crRec.Slug , crRec.Country ));
        }   
        return options;
    }
 public List<coronawrap> getperformcallout2(){
  ConsoleWrapperList2 = new List<coronawrap>();

  

HttpRequest req = new HttpRequest();

HttpResponse res = new HttpResponse();

Http http = new Http();
    

req.setEndpoint('https://api.covid19api.com/total/country/'+str);

req.setMethod('GET');

res = http.send(req);

if(res.getstatusCode() == 200 && res.getbody() != null){

ConsoleWrapperList2=(List<coronawrap>)json.deserialize(res.getbody(),List<coronawrap>.class);

}
    
return ConsoleWrapperList2;

}
public void open()
    {
      ConsoleWrapperList2 = new List<coronawrap>();
       for(coronawrap c : ConsoleWrapperList)
           ConsoleWrapperList2.add(c); 
    }
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Himanshu,

There are two ways of doing this below is the link to documentation I could find: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing.htm

>> An example link I found is: https://apexcoder.com/2019/04/30/test-class-for-apex-rest-callout/

I hope this helps and in case if this comes handy can you please choose this as best answer so that it can be used by others in the future.

Regards,
Anutej
ANUTEJANUTEJ (Salesforce Developers) 
You can not make reference to a static variable from a non-static method. To understand this, you need to understand the difference between static and non-static.

Static variables are class variables, they belong to the class with their only one instance, created at the first only. Non-static variables are initialized every time you create an object of the class.

Now, coming to your question, when you use new() operator we will create a copy of every non-static filed for every object, but it is not the case for static fields. That's why it gives compile time error if you are referencing a static variable from a non-static method.

An example is shown in this link: https://success.salesforce.com/answers?id=9063A000000lJD0QAM
ANUTEJANUTEJ (Salesforce Developers) 
So, as I said earlier the only way we can achieve cent percent code coverage is to make sure all the conditions are tested and all the required results you wanted to get by hitting the endpoint are checked.

I hope this makes sense.