コールアウトするようなApexはたまにしか書かないのでいつもMockの書き方を忘れてしまいます。
HttpCalloutMock インターフェースの実装による HTTP コールアウトのテスト
ちゃんと偽の応答で必要な値をセットしておくのを忘れてしまいます。
@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest req) {
// Optionally, only send a mock response for a specific endpoint
// and method.
System.assertEquals('https://example.com/example/test', req.getEndpoint());
System.assertEquals('GET', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"example":"test"}');
res.setStatusCode(200);
return res;
}
}
関連する質問
Mockで返すJsonが大切な例。
How to write a callout of testclass of below code
res.setBody('{"result":
[{"userType":null,"username":"username","storeName":null,"status":null,"roleName":null,"phoneNumber":null,
"personId":null,"name":"name","loginPin":null,"email":null,"createdAt":null}] }');
データClass
public class UserParser {
@AuraEnabled
public List<result> result;
public class result{
@AuraEnabled
public String createdAt ;
@AuraEnabled
public String email ;
@AuraEnabled
public String name ;
@AuraEnabled
public Integer personId ;
@AuraEnabled
public String phoneNumber;
@AuraEnabled
public String status ;
@AuraEnabled
public String storeName ;
@AuraEnabled
public String userType ;
@AuraEnabled
public String username;
@AuraEnabled
public String loginPin;
@AuraEnabled
public String roleName;
}
public static UserParser parse(String json){
return(UserParser) System.JSON.deserialize(json, UserParser.class);
}
}
コールアウトapex
public class UserController {
@AuraEnabled
public static UserParser getUserData(){
string baseURL = 'http://3.129.57.11:8443/userManagement/getAllUser';
HTTPRequest request = new HTTPRequest();
request.setEndpoint(baseURL);
request.setMethod('GET');
HTTP H = new HTTP();
HttpResponse response = h.send(request);
UserParser prsr = UserParser.parse(response.getBody());
system.debug('..'+prsr);
return prsr;
}
public static UserParser findbyname(String searchkey){
String name = '%' +searchkey+ '%';
string baseURL = 'http://3.129.57.11:8443/userManagement/getAllUser?searchString=';
baseUrl = baseUrl+searchkey;
HTTPRequest request = new HTTPRequest();
request.setEndpoint(baseURL);
request.setMethod('GET');
HTTP H = new HTTP();
HttpResponse response = h.send(request);
UserParser prsr = UserParser.parse(response.getBody());
system.debug(prsr);
return prsr;
}
}
Mock
public class UserControllerMock implements HttpCalloutMock {
// Implement this interface method
public HTTPResponse respond(HTTPRequest req) {
// Optionally, only send a mock response for a specific endpoint
// and method.
//System.assertEquals('http://3.129.57.11:8443/userManagement/getAllUser', req.getEndpoint());
System.assertEquals('GET', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"result": [{"userType":null,"username":"username","storeName":null,"status":null,"roleName":null,"phoneNumber":null,"personId":null,"name":"name","loginPin":null,"email":null,"createdAt":null}] }');
res.setStatusCode(200);
return res;
}
}
テストclass
@isTest
public class UserController_test {
@isTest
static void test01() {
Test.setMock(HttpCalloutMock.class, new UserControllerMock());
Test.startTest();
UserParser up = UserController.getUserData();
system.debug(Logginglevel.INFO,'===== '+ up );
Test.stopTest();
}
@isTest
static void test02() {
Test.setMock(HttpCalloutMock.class, new UserControllerMock());
Test.startTest();
UserParser up = UserController.findbyname('test');
system.debug(Logginglevel.INFO,'===== '+ up );
Test.stopTest();
}
}