public class ParkLocator {
public static String[] country(String country) {
ParkService.ParksImplPort service = new ParkService.ParksImplPort();
return service.byCountry(country);
}
}
Create a test class named ParkLocatorTest and create unit tests to cover all lines of code included in the ParkLocator class, resulting in 100% code coverage.
@isTest
public class ParkLocatorTest {
@isTest static void testCountry() {
ParkServiceMock mock = new ParkServiceMock();
Test.setMock(WebServiceMock.class, mock);
Test.startTest();
String[] result = ParkLocator.country('US');
Test.stopTest();
System.assertEquals(2, result.size);
System.assertEquals('Yellowstone', result[0]);
System.assertEquals('Yosemite', result[1]);
}
}
Remember to wrap the actual callout in `Test.startTest()` and `Test.stopTest()` to make sure it is included in the asynchronous processing and properly measured against governor limits.
The next step is to create a mock class called ParkServiceMock that implements the WebServiceMock interface.
@isTest
global class ParkServiceMock implements WebServiceMock {
global void doInvoke(
Object stub,
Object request,
Map response,
String endpoint,
String soapAction,
String requestName,
String responseNS,
String responseName,
String responseType) {
ParkService.byCountryResponse res = new ParkService.byCountryResponse();
res.return_x = new String[]{'Yellowstone', 'Yosemite'};
response.put('response_x', res);
}
}