You need to sign in to do that
Don't have an account?
Akshay Juwa
How to write a test class for my class?
I'm new to the salesforce. Does anybody know how to write a test class for the following?
public class JokeApiService {
public static List<JokeApiService.JokeWrapper> getJoke(String type) {
String url = System.Label.Joke_API_URL;
try {
Http http = new Http();
HttpRequest req = new HttpRequest();
//10 is the max number of jokes that can be called at once
req.setEndpoint(url+type+'?type=single&safe-mode&amount=10');
req.setMethod('GET');
HttpResponse res = http.send(req);
Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
String serializedJokes = JSON.serialize(results.get('jokes'));
List<JokeApiService.JokeWrapper> jokes = (List<JokeApiService.JokeWrapper>) JSON.deserialize(serializedJokes, List<JokeApiService.JokeWrapper>.class);
return jokes;
} catch (Exception e) {
System.debug(e.getMessage());
return null;
}
}
public class JokeWrapper {
public String category;
public String type;
public String joke;
}
}
public class JokeApiService {
public static List<JokeApiService.JokeWrapper> getJoke(String type) {
String url = System.Label.Joke_API_URL;
try {
Http http = new Http();
HttpRequest req = new HttpRequest();
//10 is the max number of jokes that can be called at once
req.setEndpoint(url+type+'?type=single&safe-mode&amount=10');
req.setMethod('GET');
HttpResponse res = http.send(req);
Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
String serializedJokes = JSON.serialize(results.get('jokes'));
List<JokeApiService.JokeWrapper> jokes = (List<JokeApiService.JokeWrapper>) JSON.deserialize(serializedJokes, List<JokeApiService.JokeWrapper>.class);
return jokes;
} catch (Exception e) {
System.debug(e.getMessage());
return null;
}
}
public class JokeWrapper {
public String category;
public String type;
public String joke;
}
}
Can you provide details llike what is the url that you have in Joke_API_URL Label?
Thanks,