0
@isTest
global class weatherMock implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"name":"San Francisco","units":"imperial"}');
        res.setStatusCode(200);
        return res;
    } 
}
@isTest
private class WeatherControllerTest {
    @isTest
    private static void testWeatherCallout(){
        Account testAccount = new Account(Name='Test Account', BillingCity='San Francisco');
        insert testAccount;
        
        Test.setMock(HttpCalloutMock.class, new  weatherMock());
        Map<String, Object> response = WeatherController.weatherCallout(testAccount.Id,'metric');

        HttpRequest request = new HttpRequest();
        request.setMethod('GET');
        request.setEndpoint('https://api.openweathermap.org/data/2.5/weather');
        System.assertNotEquals(null, response);
        System.assertEquals(200, response.getStatusCode());
    }
}

the error is :

Method does not exist or incorrect signature: void getStatusCode() from the type Map<String,Object>

1
  • 1
    Firstly, about your post: Please do not post code or error text exclusively as screenshots - it is not as accessible to users who use screen readers or other assistive technology. (Plus it makes it harder for anyone to help if they want to copy your code and put it in a sandbox or code editor.) Instead, copy your code and paste it in the SFSE editor with three backticks (```) above and below it in order to format it as a code block. Similarly, format error text with a greater than symbol (>) at the beginning to format it as a quote block.
    – Moonpie
    Jan 8, 2023 at 15:54

2 Answers 2

2

Map<,> class doesn't contain getStatusCode() method.

Use HTTPResponse class and make response.getStatusCode().

You have to update WeatherController.weatherCallout(testAccoount.Id, 'metric') row. Make it like this:

HTTPResponse response = WeatherController.weatherCallout(testAccoount.Id, 'metric')

It should work.

3
  • 1
    hi Konstantin i did what so said but it show s(Illegal assignment from Map<String,Object> to System.HttpResponse)
    – spenceryan
    Jan 8, 2023 at 16:11
  • Could you please share WeatherController.weatherCallout source code then?
    – Konstantin
    Jan 8, 2023 at 18:48
  • I believe the reason inside WeatherController.weatherCallout method. Probably you are process response inside WeatherController.weatherCallout method and return Map. In this case you shouldn't check status code in the test. You should check data which you put in the Map. Or you are able to make a method which return HttpResponse(test it separately) and test current WeatherController.weatherCallout method without HttpResponse
    – Konstantin
    Jan 8, 2023 at 19:00
0
private static void testWeatherCallout(){
    // ...
    HttpRequest request = new HttpRequest();
    request.setMethod('GET');
    request.setEndpoint('https://api.openweathermap.org/data/2.5/weather');
    System.assertNotEquals(null, response);
    System.assertEquals(200, response.getStatusCode());
}

None of that code is necessary. Presumably, WeatherController.weatherCallout already received the data from your mock. Your unit test just needs to confirm you got the expected data:

Assert.areEqual(
  'San Francisco', 
  response.get('name'), 
  'Expected the payload to contain the name San Franciso'
);

You must log in to answer this question.

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