Apex SOAP Callouts - Generate an Apex class using WSDL2Apex and write a test class.
Generate an Apex class using WSDL2Apex for a SOAP web service, write unit tests that achieve 100% code coverage for the class using a mock response, and run your Apex tests.
- Use WSDL2Apex to generate a class called 'ParkService' in public scope using this WSDL file. After you click the 'Parse WSDL' button don't forget to change the name of the Apex Class Name from 'parksServices' to 'ParkService'.
- Create a class called 'ParkLocator' that has a 'country' method that uses the 'ParkService' class and returns an array of available park names for a particular country passed to the web service. Possible country names that can be passed to the web service include Germany, India, Japan and United States.
- Create a test class named ParkLocatorTest that uses a mock class called ParkServiceMock to mock the callout response.
- The unit tests must cover all lines of code included in the ParkLocator class, resulting in 100% code coverage.
- Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
ParkLocator.apxc
public class ParkLocator {
public static List<String> country(String country){
ParkService.ParksImplPort parkService = new ParkService.ParksImplPort();
return parkService.byCountry(country);
}
}
ParkServiceMock.apxc
@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
List<String> parks = new list<String>();
parks.add('Yosemite');
parks.add('Yellowstone');
parks.add('Another Parks');
ParkService.byCountryResponse response_x =
new ParkService.byCountryResponse();
response_x.return_x = parks;
// end
response.put('response_x', response_x);
}
}
ParkLocatorTest.apxc
@isTest
public class ParkLocatorTest {
@isTest static void testCallout() {
// This causes a fake response to be generated
Test.setMock(WebServiceMock.class, new ParkServiceMock());
// Call the method that invokes a callout
String country = 'India';
List<String> result = ParkLocator.country(country);
List<String> parks = new list<String>();
parks.add('Yosemite');
parks.add('Yellowstone');
parks.add('Another Parks');
// Verify that a fake result is returned
System.assertEquals(parks, result);
}
}
Comments
Post a Comment