+ Start a Discussion
DeepikareddyDeepikareddy 

Testmockcallout in salesforce

error as //Non static method cannot be referenced from a static context: System.PageReference errp.callouttest4.test1();


 Apex class:
public class callouttest4 {
    
    public map<string, object> results{get;set;}
    public list<object>animals{get;set;}
    public static string teststring{get;set;}
   public  PageReference   test1(){
       
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('GET');
 
   try {
         HttpResponse response = http.send(request);
             if (response.getStatusCode() != 200) {
                 System.debug('The status code returned was not expected: ' +
                 response.getStatusCode() + ' ' + response.getStatus());
             } else {
                System.debug(response.getStatusCode());
                 System.debug(response.getBody());
                 
                 teststring = response.getBody();
   results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
    system.debug(results);
    animals = (List<Object>) results.get('animals');
    System.debug('Received the following animals:');
                 for (Object animal: animals) {
                System.debug(animal);
                    }
          }
         }catch (Exception e) {
  System.debug(e);
       }   
     return null;    
    }
    
    
}

mockcallout:
//callouttest4mock;

@isTest
global class callouttest4mock 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('http://example.com/example/test', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"example":"test"}');
        res.setStatusCode(200);
        return res;
    }
}

finalTest :
 
@isTest
public class callouttest4test {
    
    @isTest Static Void testCallout(){
        
system.Test.setMock(HttpCalloutMock.class, new callouttest4mock());
     
      callouttest4  t4 = new callouttest4();
        
         t4.test1();
        
               HttpResponse res = callouttest4.test1();

     //should to get the apex class response.. how can it acheived , thanks 
     //getting error as Non static method cannot be referenced from a static context: System.PageReference errp.callouttest4.test1()
    }

}

//Cant we use it using the pageRefernce .. !
Krishna.AvvaKrishna.Avva
Hi, 

Your APEX Class callouttest4  is non static which you are trying to call from a static test class. Hence the error. 
Remove the static keyword from your test class. 
DeepikareddyDeepikareddy
Hi krishna , how to proceed with further step...! for test class static key word is mandatory itseems.. 

 
@isTest
public class callouttest4test {
    
    @isTest Static Void testCallout(){
        
system.Test.setMock(HttpCalloutMock.class, new callouttest4mock());
     
      callouttest4  t4 = new callouttest4();
         t4.test1();
        
       // HttpResponse res = ???
      
    
     //getting error as Non static method cannot be referenced from a static context: System.PageReference errp.callouttest4.test1()
    }

}

 
Deepali KulshresthaDeepali Kulshrestha
Hi Deepika,

I've gone through your code and you should do below changes inside your code:


1.(Note---> You are calling the (test1 method by using the class name (i.e. callouttest4) , But your method is not static )).
2. So there is 2 Scenario either make the method (test1) static OR create an object of (callouttest4) and call (test1) method.

  -->HttpResponse res = callouttest4.test1(); //make changes here 
  
  
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com