function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Florian Dardelet 7Florian Dardelet 7 

How to write a test class for a manual SOAP callout?

Hello,

I have to integrate with a SOAP API which isn't supported with WSDL2Apex. I have done the integration manually and it's working, but I'm stuck on getting the test class to mock the webservice callout. 

The method which invokes the webservice is defined below. The method takes 2 inputs: 
    webServiceCall__c contains the actual request details, including the payload which has been computed beforehand
    credentials__c contains the details for the webservice, including among others the endpoint and soapaction to use. 

The purpose of this method is to retrieve the response body and return it for interpretation.
 

public class FQDwebService{

	public static String invokeFQD(webServiceCall__c request, credentials__c credentials){

        HttpRequest req = new HttpRequest();
        req.setTimeout(30000);
        req.setEndpoint(credentials.endpoint__c);
        req.setMethod('POST');
        req.setBody(request.payloadRQ__c);
        req.setHeader('SOAPAction', credentials.headerSOAPAction__c);
        Http http = new Http();
        system.debug('Sending RQ');
        HTTPResponse res = http.send(req);
        
        return res.getBody();
    }	
}
I have tried various combinations but it just doesn't work. How should I write a mock and test class to get this working? 

Current Mock
@isTest
global class FQDwebServiceMock 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){
                            string response_x = '<body>Success</body>';
                                 response.put('response_x',response_x);
                         }
}

And current test class
@isTest 
static void invokeFQDwebService(){
        Test.setMock(WebServiceMock.class, new FQDwebServiceMock());
		
		
        testDataFactory.generateTestData();
        webServiceCall__c testRequest = [SOQL retrieving the test request];
        
		credentials__c testCredentials = new credentials__c(
            headerSOAPAction__c = 'SOAPAction',
            endpoint__c = 'https://endpoint'
        );
        
        
        String testString = FQDwebService.invokeFQD(testRequest, testCredentials);
        String targetString = '<body>Success</body>';
        System.assertEquals(targetString, testString);
        
    }

Thank you for any help you can provide.
 



 

AnkaiahAnkaiah (Salesforce Developers) 
Hi Florian,

Refer the below link will help you to proceed further on your test class.
https://www.infallibletechie.com/2016/08/test-class-for-soap-callout-in.html
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm

Thanks!!