Apex SOAP コールアウト

trailhead.salesforce.com

1.

WSDL からの生成 > 「ParkService」
登録;ParkService.xml

2.

class ① 作成 「ParkLocator.apex」
xmlのメソッドを呼び出すApex

public class ParkLocator {
    public static List<String> country(String pCountry){
        ParkService.ParksImplPort  park = new ParkService.ParksImplPort();
        return park.byCountry(pCountry);
    }
}

3.

class ② 作成 「ParkServiceMock.apex」
class ①で呼び出したメソッドの結果を指定するモック

@isTest
global class ParkServiceMock implements WebSErviceMock{
    global void doInvoke(
        Object stub,
        Object request,
        Map<String, Object> response,
        String endpoint,
        String soapAction,
        String requestName,
        String responseNS,
        String responseName,
        String responseType) {
            //start - specify the response you want to send
            ParkService.byCountryResponse response_x =
                new ParkService.byCountryResponse();
            //Germany, India, Japan and United States.
            List<String> countries = new List<String>();
            countries.add('Yoyogi');
            countries.add('Hibiya');
            countries.add('Ueno');
            response_x.return_x = countries;
            //end
            response.put('response_x',response_x);
            
        }
}

補足:
1.
global
2.
implements WebSErviceMockのため、パラメータは
(
Object stub,
Object request,
Map response,
String endpoint,
String soapAction,
String requestName,
String responseNS,
String responseName,
String responseType)
これで決まり。

4.

class ③ 作成 「ParkLocatorTest.apex」
class ① をテストするためのApex

@isTest
private class ParkLocatorTest {
    @isTest static void testCallout(){
        //This causes a fake response to be generated
        Test.setMock(WebSErviceMock.class, new ParkServiceMock());
        //Call the method that invikese to be generated
        String country = 'Japan';
        List<String> result = ParkLocator.country(country);
        
        List<String> countries = new List<String>();
        countries.add('Yoyogi');
        countries.add('Hibiya');
        countries.add('Ueno');
        System.assertEquals(countries,result);
    }
}


大きい概念は
class ①をテストするためにclass ③ を作るが、
これだけではテストがうまく行かないため、
class ②の仮想の結果を指定しておく。