ขนาดวิดีโอ: 1280 X 720853 X 480640 X 360
แสดงแผงควบคุมโปรแกรมเล่น
เล่นอัตโนมัติ
เล่นใหม่
public class AnimalLocator{ public static String getAnimalNameById(Integer y){ Http http = new Http(); HttpRequest req = new HttpRequest(); req.setEndpoint('th-apex-http-callout.herokuapp.com/animals/' + y); req.setMethod('GET'); Map animal= new Map(); HttpResponse response = http.send(req); if (response.getStatusCode() == 200) { Map results = (Map)JSON.deserializeUntyped(response.getBody()); animal = (Map) results.get('animal'); }return (String)animal.get('name'); }}
@isTest private class AnimalLocatorTest { @isTest static void AnimalLocatorMock1(){ // Set mock callout class Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock()); string result = AnimalLocator.getAnimalNameById(3); string expectedValue = 'elephant'; System.assertEquals(result, expectedValue); }}
@isTestglobal class AnimalLocatorMock implements HttpCalloutMock { // Implement this interface method global HTTPResponse respond(HTTPRequest request) { // Create a fake response HttpResponse response = new HttpResponse(); response.setHeader('Content-Type', 'application/json'); response.setBody('{"animals": ["goat", "lion", "tiger", "elephant", "chicken"]}'); response.setStatusCode(200); return response; }}
public class AnimalLocator{
public static String getAnimalNameById(Integer y){
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('th-apex-http-callout.herokuapp.com/animals/' + y);
req.setMethod('GET');
Map animal= new Map();
HttpResponse response = http.send(req);
if (response.getStatusCode() == 200) {
Map results = (Map)JSON.deserializeUntyped(response.getBody());
animal = (Map) results.get('animal');
}
return (String)animal.get('name');
}
}
@isTest
private class AnimalLocatorTest {
@isTest static void AnimalLocatorMock1(){
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
string result = AnimalLocator.getAnimalNameById(3);
string expectedValue = 'elephant';
System.assertEquals(result, expectedValue);
}
}
@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest request) {
// Create a fake response
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody('{"animals": ["goat", "lion", "tiger", "elephant", "chicken"]}');
response.setStatusCode(200);
return response;
}
}