0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

HttpCalloutMock インターフェースの実装による HTTP コールアウトのテスト

Last updated at Posted at 2022-08-10

コールアウトするような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();        
    }
    
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?