@isTest
global class MyWebServiceCalloutMock implements WebServiceMock
{
// Need to override this method
global void doInvoke(
Object stub,
Object request, Map<String, Object> response, String endpoint, String soapAction, String requestName, String responseNS, String responseName, String responseType){
try
{
MyService.myElement test = MyService.MyElement();
MyElement.Result = 'Test';
response.put('response_x', test);
}catch(Exception ex)
{
system.debug(ex.getMessage() + ' ' + ex.getStackTraceString());
}
return res;
}
}
2. Create the test class
@isTest public class WebServiceTest { public static testMethod void testRestGetService() { try { Test.startTest(); Test.setMock(WebServiceMock.class, new MyWebserviceCalloutMock()); String response = MyCalloutClass.processService('Test'); // provide your assertions here Test.stopTest(); } catch(Exception ex) { system.debug(ex.getMessage() + ' ' + ex.getStackTraceString()); } } }This is the sample Callout Class public class MyCalloutClass { public static String processService(String input) {
try
{
MyService.MyElement elm = new MyService.MyElement();
elm.endpoint_c = 'your endpoint url';
return elm.myServiceMethod(input);
}
catch(Exception ex)
{
system.debug(ex.getMessage() + ' ' + ex.getStackTraceString());
}
return '';
}
}
This is a sample generated class
public class MyService
{
public class MyElement
{
public static String Result;
public String myServiceMethod(String input)
{
// Autogenerated code here
}
}
}
You can find more information to the original article here:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_gen_code.htm
No comments:
Post a Comment