Generated code is saved as an Apex class containing the methods you can invoke for calling the web service. To deploy or package this Apex class and other accompanying code, 75% of the code must have test coverage, including the methods in the generated class. By default, test methods don’t support web service callouts, and tests that perform web service callouts fail. To prevent tests from failing and to increase code coverage, Apex provides the built-in WebServiceMockinterface and the Test.setMock method. Use WebServiceMock and Test.setMock to receive fake responses in a test method.
Specify a Mock Response for Testing Web Service Callouts
When you create an Apex class from a WSDL, the methods in the auto-generated class call WebServiceCallout.invoke, which performs the callout to the external service. When testing these methods, you can instruct the Apex runtime to generate a fake response whenever WebServiceCallout.invoke is called. To do so, implement the WebServiceMock interface and specify a fake response for the Apex runtime to send. Here are the steps in more detail.
First, implement the WebServiceMock interface and specify the fake response in the doInvoke method.
01 | global class YourWebServiceMockImpl
implements WebServiceMock { |
05 | Map< String , Object> response, |
11 | String responseType) { |
16 | response.put( 'response_x' , responseElement); |
Now that you have specified the values of the fake response, instruct the Apex runtime to send this fake response by calling Test.setMock in your test method. For the first argument, pass WebServiceMock.class, and for the second argument, pass a new instance of your interface implementation of WebServiceMock, as follows:
1 | Test.setMock(WebServiceMock. class , new YourWebServiceMockImpl ());
|
After this point, if a web service callout is invoked in test context, the callout is not made. You receive the mock response specified in your doInvoke method implementation.
This example shows how to test a web service callout. The implementation of the WebServiceMock interface is listed first. This example implements the doInvoke method, which returns the response you specify. In this case, the response element of the auto-generated class is created and assigned a value. Next, the response Map parameter is populated with this fake response. This example is based on the WSDL listed in Generated WSDL2Apex Code. Import this WSDL and generate a class called docSample before you save this class.
02 | global class WebServiceMockImpl implements WebServiceMock { |
06 | Map< String , Object> response, |
12 | String responseType) { |
13 | docSample.EchoStringResponse_element respElement = |
14 | new docSample.EchoStringResponse_element(); |
15 | respElement.EchoStringResult = 'Mock response' ; |
16 | response.put( 'response_x' , respElement); |
This method makes a web service callout.
01 | public class WebSvcCallout { |
02 | public static String callEchoString( String input) { |
03 | docSample.DocSamplePort sample = new docSample.DocSamplePort(); |
07 | String echo = sample.EchoString(input); |
This test class contains the test method that sets the mock callout mode. It calls the callEchoString method in the previous class and verifies that a mock response is received.
02 | private class WebSvcCalloutTest { |
03 | @isTest static void testEchoString() { |
05 | Test.setMock(WebServiceMock. class , new WebServiceMockImpl()); |
08 | String output = WebSvcCallout.callEchoString( 'Hello World!' ); |
11 | System .assertEquals( 'Mock response' , output); |
No comments:
Post a Comment