I'm new to Salesforce and now I'm struggling with one task. I need to make test class, but I'm wondering: how do you cover the second @auraEnabled method with If statement? Here is the code:
Apex Class:
public with sharing class MyWeatherCallout {
public static Httpresponse GetWeatherCallout(String city) {
Http Http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://api.openweathermap.org/data/2.5/weather?APPID=4452fe32937db97f4ba988929a52290e&q=' + city + '&units=metric');
request.setMethod('GET');
HttpResponse response = Http.send(request);
return response;
}
@AuraEnabled
public static String getjson(String city) {
System.debug(city);
if (city == null || city== '') {
id profileid = UserInfo.getProfileId();
User User = [select city from User where profileid =: profileid];
city = User.city;
if (city == null || city == '') {
Default_City__c defobj = [Select Default_City_Field__c from Default_City__c];
city = defobj.Default_City_Field__c;
}
}
String mystring = GetWeatherCallout(city).getBody();
System.debug(mystring);
return mystring;
}
}
Test class:
@isTest
private class MyWeatherTestClass {
@isTest
static void testGetCallout() {
StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
mock.setStaticResource('OpenWeatherMap');
mock.setStatusCode(200);
mock.setHeader('Content-Type', 'application/json;charset=UTF-8');
Test.setMock(HttpCalloutMock.class, new WeatherHttpCalloutMock());
HttpResponse result = MyWeatherCallout.GetWeatherCallout();
System.assertNotEquals(null, result,
'The callout returned a null response.');
System.assertEquals(200, result.getStatusCode(),
'The status code is not 200.');
System.assertEquals('application/json;charset=UTF-8',
result.getHeader('Content-Type'),
'The content type value is not expected.');
}
}