1

This is my rest api class,in this class need to write mock test class

global class Searchaddress {

@RemoteAction

 global static list<String> restapi(string accName){

        string jsonStr;  

        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Accept','application/JSON');
        req.setEndpoint('http://api-adresse.data.gouv.fr/search/?q='+ EncodingUtil.urlEncode(accName,'UTF-8'));//+'&'+'limit'+'='+'10');// 
        req.setMethod('GET');
        HttpResponse res = h.send(req);
       // List<String> calOut = new List<String>();
       // jsonStr= res.getBody();
        system.debug('res1===>'+res.getBody());
      //  JSONParser parser = JSON.createParser(jsonStr);

        List<String> calOut1 = new List<String>();

          JSON2Apex parsed = JSON2Apex.parse(res.getBody());
          for (JSON2Apex.Features f : parsed.Features) 
          {
              JSON2Apex.Properties p = f.Properties ;

               // calOut1.add(p.label+ ', ' + p.context+ ',' + p.city+ ', ' + p.postcode);
              calOut1.add(p.label+' '+'FRANCE');




          }
          return calOut1;
    }
 }

This is my mock test class:

@isTest
private class Rfleet_CalloutClassTest {
     @isTest static void testCallout() {
            Test.setMock(HttpCalloutMock.class, new Rfleet_MockHttpResponseGenerator());


         HttpResponse res = Searchaddress.restapi('france');

        // Verify response received contains fake values
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
    }
}

I am getting error "Error: Compile Error: Illegal assignment from List to System.HttpResponse at line 7 column 23" how to resolve this error?

1 Answer 1

2

The issue is your method has return type of List and you test class is equating it with a HTTP Response.

 HttpResponse res = Searchaddress.restapi('france');

If you replace the above line with -

List<String> responseList = Searchaddress.restapi('france');

Then this error will be eliminated!

4
  • thanks for your quick reply,i changed as per your suggest,but getting next line error
    – Nikitha
    Commented Aug 17, 2015 at 7:56
  • Error: Compile Error: Method does not exist or incorrect signature: [List<String>].getHeader(String) at line 12 column 30
    – Nikitha
    Commented Aug 17, 2015 at 7:57
  • String contentType = res.getHeader('Content-Type'); System.assert(contentType == 'application/json'); String actualValue = res.getBody(); String expectedValue = '{"foo":"bar"}'; System.assertEquals(actualValue, expectedValue); System.assertEquals(200, res.getStatusCode()); All these lines will be incorrect. You will have to apply a single system.asset on the return value for the class like - what is expected out of List<String>() and what you get! Commented Aug 17, 2015 at 8:07
  • did it work as expected? Commented Aug 17, 2015 at 11:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .