+ Start a Discussion
Gaurav AgnihotriGaurav Agnihotri 

Test class for WSDLToApex Class

I was able to create a apex classes from WSDL. Now, I need to create a test class for code coverage. This requires doing a mock call to the web service. I am having difficultly creating test classes.
  1. First I started with a class that implements WebServiceMock. In this class I created a response element, populated it and add response element.
@isTest
global class WebServiceTestMock 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)
	{

		//Create Response element form the autogenerated class.
		tempuriOrg.ProcessWarehouseInventoryResponse_element request_x = new tempuriOrg.ProcessWarehouseInventoryResponse_element();
		//populate response element
		tempuriOrg.ProcessWarehouseInventoryResponse_element response_x;
		//Add response element to the reponse parameter
		response.put('response_x', response_x);

	}
}
 2. ​ Next, I created a new class that populates end point and invokes EchoString Method
public class WebServiceCallout
{
	public static String callEchoString(String input)
	{

		
		tempuriOrg.QantelDataServiceEndPoint.endpoint_x = 'http://api.salesforce.com/foo/bar';

		// This invokes the EchoString method in the generated class
		String echo = sample.EchoString(input);

		return echo;
	}

}

3. Finally, I created a test class to fake a response to be generated
@isTest
private class WebSvcCalloutTest
{
	@isTest static void testEchoString()
	{
		// This causes a fake response to be generated
		Test.setMock(WebServiceMock.class, new WebServiceTestMock());

		// Call the method that invokes a callout
		//String output = WebServiceCallout.callEchoString('Hello World!');
		
		// Verify that a fake result is returned
		//System.assertEquals('Mock response', output);
	}


}
Please help me out or point me to the right resource !!

Regards, 
Gaurav
Best Answer chosen by Gaurav Agnihotri
Daniel BallingerDaniel Ballinger
Hi Gaurav,

This looks like you generated it using the FuseIT SFDC Explorer Wsdl2Apex tool (http://www.fuseit.com/explorer). Part of this tool includes the option to generate the Mock and associated test classes for you. It won't be an exhaustive test of the web service, but it will give you coverage of the generated code.

On the Tab where you are selecting the methods to generate code for, check the "Generate Test Classes" button in the toolbar.

User-added image

From your example code, you want something like this in your test case to call the web service.
 
@isTest
private class WebSvcCalloutTest
{
    @isTest static void testEchoString()
    {
        // This causes a fake response to be generated
        Test.setMock(WebServiceMock.class, new WebServiceTestMock());

        // Call the method that invokes a callout
        tempuriOrg.QantelDataServiceEndPoint ws = new tempuriOrg.QantelDataServiceEndPoint();
        schemasDatacontractOrg200407PelcoSa.ProcessWarehouseInventoryDataModel output = ws.ProcessWarehouseInventory('username', 'password');

        // Verify that a fake result is returned
        //System.assertEquals('Mock response', output);
    }


}



 

All Answers

Gaurav AgnihotriGaurav Agnihotri
Oops!! forgot to incude WSDL generated class.

//Methods Included: ProcessAccounts, ProcessExchangeRates, ProcessPricebookItems, ProcessProductCatalogItems, ProcessSpecialDiscounts, ProcessWarehouseInventory
// Primary Port Class Name: QantelDataServiceEndPoint	
public class tempuriOrg {
	public class ProcessAccounts_element {
		public String username;
		public String password;
		private String[] username_type_info = new String[]{'username','http://tempuri.org/','string','0','1','true'};
		private String[] password_type_info = new String[]{'password','http://tempuri.org/','string','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'username','password'};
	}
	public class ProcessAccountsResponse_element {
		public schemasDatacontractOrg200407PelcoSa.ProcessAccountsDataModel ProcessAccountsResult;
		private String[] ProcessAccountsResult_type_info = new String[]{'ProcessAccountsResult','http://tempuri.org/','ProcessAccountsDataModel','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'ProcessAccountsResult'};
	}
	public class ProcessExchangeRates_element {
		public String username;
		public String password;
		private String[] username_type_info = new String[]{'username','http://tempuri.org/','string','0','1','true'};
		private String[] password_type_info = new String[]{'password','http://tempuri.org/','string','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'username','password'};
	}
	public class ProcessExchangeRatesResponse_element {
		public schemasDatacontractOrg200407PelcoSa.ProcessExchangeRatesDataModel ProcessExchangeRatesResult;
		private String[] ProcessExchangeRatesResult_type_info = new String[]{'ProcessExchangeRatesResult','http://tempuri.org/','ProcessExchangeRatesDataModel','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'ProcessExchangeRatesResult'};
	}
	public class ProcessPricebookItems_element {
		public String username;
		public String password;
		private String[] username_type_info = new String[]{'username','http://tempuri.org/','string','0','1','true'};
		private String[] password_type_info = new String[]{'password','http://tempuri.org/','string','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'username','password'};
	}
	public class ProcessPricebookItemsResponse_element {
		public schemasDatacontractOrg200407PelcoSa.ProcessPricebookItemsDataModel ProcessPricebookItemsResult;
		private String[] ProcessPricebookItemsResult_type_info = new String[]{'ProcessPricebookItemsResult','http://tempuri.org/','ProcessPricebookItemsDataModel','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'ProcessPricebookItemsResult'};
	}
	public class ProcessProductCatalogItems_element {
		public String username;
		public String password;
		private String[] username_type_info = new String[]{'username','http://tempuri.org/','string','0','1','true'};
		private String[] password_type_info = new String[]{'password','http://tempuri.org/','string','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'username','password'};
	}
	public class ProcessProductCatalogItemsResponse_element {
		public schemasDatacontractOrg200407PelcoSa.ProcessProductCatalogItemsDataModel ProcessProductCatalogItemsResult;
		private String[] ProcessProductCatalogItemsResult_type_info = new String[]{'ProcessProductCatalogItemsResult','http://tempuri.org/','ProcessProductCatalogItemsDataModel','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'ProcessProductCatalogItemsResult'};
	}
	public class ProcessSpecialDiscounts_element {
		public String username;
		public String password;
		private String[] username_type_info = new String[]{'username','http://tempuri.org/','string','0','1','true'};
		private String[] password_type_info = new String[]{'password','http://tempuri.org/','string','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'username','password'};
	}
	public class ProcessSpecialDiscountsResponse_element {
		public schemasDatacontractOrg200407PelcoSa.ProcessSpecialDiscountsDataModel ProcessSpecialDiscountsResult;
		private String[] ProcessSpecialDiscountsResult_type_info = new String[]{'ProcessSpecialDiscountsResult','http://tempuri.org/','ProcessSpecialDiscountsDataModel','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'ProcessSpecialDiscountsResult'};
	}
	public class ProcessWarehouseInventory_element {
		public String username;
		public String password;
		private String[] username_type_info = new String[]{'username','http://tempuri.org/','string','0','1','true'};
		private String[] password_type_info = new String[]{'password','http://tempuri.org/','string','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'username','password'};
	}
	public class ProcessWarehouseInventoryResponse_element {
		public schemasDatacontractOrg200407PelcoSa.ProcessWarehouseInventoryDataModel ProcessWarehouseInventoryResult;
		private String[] ProcessWarehouseInventoryResult_type_info = new String[]{'ProcessWarehouseInventoryResult','http://tempuri.org/','ProcessWarehouseInventoryDataModel','0','1','true'};
		private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
		private String[] field_order_type_info = new String[]{'ProcessWarehouseInventoryResult'};
	}
	public class QantelDataServiceEndPoint {
		public String endpoint_x = 'https://pelcosales.schneider-electric.com/SalesforceServices/QantelData/QantelDataService.svc';
		public Map<String,String> inputHttpHeaders_x;
		public Map<String,String> outputHttpHeaders_x;
		public String clientCertName_x;
		public String clientCert_x;
		public String clientCertPasswd_x;
		public Integer timeout_x;
		private String[] ns_map_type_info = new String[]{'http://tempuri.org/','tempuriOrg','http://schemas.microsoft.com/2003/10/Serialization/','schemasMicrosoftCom200310Serializat','http://schemas.datacontract.org/2004/07/Pelco.SalesForceIntegration.WCFServiceHost','schemasDatacontractOrg200407PelcoSa'};

		public schemasDatacontractOrg200407PelcoSa.ProcessAccountsDataModel ProcessAccounts(String username,String password) {
			tempuriOrg.ProcessAccounts_element request_x = new tempuriOrg.ProcessAccounts_element();
			tempuriOrg.ProcessAccountsResponse_element response_x;
			request_x.username = username;
			request_x.password = password;
			Map<String, tempuriOrg.ProcessAccountsResponse_element> response_map_x = new Map<String, tempuriOrg.ProcessAccountsResponse_element>();
			response_map_x.put('response_x', response_x);
			WebServiceCallout.invoke(
				this,
				request_x,
				response_map_x,
				new String[]{endpoint_x,
				'http://tempuri.org/IQantelDataService/ProcessAccounts',
				'http://tempuri.org/',
				'ProcessAccounts',
				'http://tempuri.org/',
				'ProcessAccountsResponse',
				'tempuriOrg.ProcessAccountsResponse_element'}
			);
			response_x = response_map_x.get('response_x');
			return response_x.ProcessAccountsResult;
		}

		public schemasDatacontractOrg200407PelcoSa.ProcessExchangeRatesDataModel ProcessExchangeRates(String username,String password) {
			tempuriOrg.ProcessExchangeRates_element request_x = new tempuriOrg.ProcessExchangeRates_element();
			tempuriOrg.ProcessExchangeRatesResponse_element response_x;
			request_x.username = username;
			request_x.password = password;
			Map<String, tempuriOrg.ProcessExchangeRatesResponse_element> response_map_x = new Map<String, tempuriOrg.ProcessExchangeRatesResponse_element>();
			response_map_x.put('response_x', response_x);
			WebServiceCallout.invoke(
				this,
				request_x,
				response_map_x,
				new String[]{endpoint_x,
				'http://tempuri.org/IQantelDataService/ProcessExchangeRates',
				'http://tempuri.org/',
				'ProcessExchangeRates',
				'http://tempuri.org/',
				'ProcessExchangeRatesResponse',
				'tempuriOrg.ProcessExchangeRatesResponse_element'}
			);
			response_x = response_map_x.get('response_x');
			return response_x.ProcessExchangeRatesResult;
		}

		public schemasDatacontractOrg200407PelcoSa.ProcessPricebookItemsDataModel ProcessPricebookItems(String username,String password) {
			tempuriOrg.ProcessPricebookItems_element request_x = new tempuriOrg.ProcessPricebookItems_element();
			tempuriOrg.ProcessPricebookItemsResponse_element response_x;
			request_x.username = username;
			request_x.password = password;
			Map<String, tempuriOrg.ProcessPricebookItemsResponse_element> response_map_x = new Map<String, tempuriOrg.ProcessPricebookItemsResponse_element>();
			response_map_x.put('response_x', response_x);
			WebServiceCallout.invoke(
				this,
				request_x,
				response_map_x,
				new String[]{endpoint_x,
				'http://tempuri.org/IQantelDataService/ProcessPricebookItems',
				'http://tempuri.org/',
				'ProcessPricebookItems',
				'http://tempuri.org/',
				'ProcessPricebookItemsResponse',
				'tempuriOrg.ProcessPricebookItemsResponse_element'}
			);
			response_x = response_map_x.get('response_x');
			return response_x.ProcessPricebookItemsResult;
		}

		public schemasDatacontractOrg200407PelcoSa.ProcessProductCatalogItemsDataModel ProcessProductCatalogItems(String username,String password) {
			tempuriOrg.ProcessProductCatalogItems_element request_x = new tempuriOrg.ProcessProductCatalogItems_element();
			tempuriOrg.ProcessProductCatalogItemsResponse_element response_x;
			request_x.username = username;
			request_x.password = password;
			Map<String, tempuriOrg.ProcessProductCatalogItemsResponse_element> response_map_x = new Map<String, tempuriOrg.ProcessProductCatalogItemsResponse_element>();
			response_map_x.put('response_x', response_x);
			WebServiceCallout.invoke(
				this,
				request_x,
				response_map_x,
				new String[]{endpoint_x,
				'http://tempuri.org/IQantelDataService/ProcessProductCatalogItems',
				'http://tempuri.org/',
				'ProcessProductCatalogItems',
				'http://tempuri.org/',
				'ProcessProductCatalogItemsResponse',
				'tempuriOrg.ProcessProductCatalogItemsResponse_element'}
			);
			response_x = response_map_x.get('response_x');
			return response_x.ProcessProductCatalogItemsResult;
		}

		public schemasDatacontractOrg200407PelcoSa.ProcessSpecialDiscountsDataModel ProcessSpecialDiscounts(String username,String password) {
			tempuriOrg.ProcessSpecialDiscounts_element request_x = new tempuriOrg.ProcessSpecialDiscounts_element();
			tempuriOrg.ProcessSpecialDiscountsResponse_element response_x;
			request_x.username = username;
			request_x.password = password;
			Map<String, tempuriOrg.ProcessSpecialDiscountsResponse_element> response_map_x = new Map<String, tempuriOrg.ProcessSpecialDiscountsResponse_element>();
			response_map_x.put('response_x', response_x);
			WebServiceCallout.invoke(
				this,
				request_x,
				response_map_x,
				new String[]{endpoint_x,
				'http://tempuri.org/IQantelDataService/ProcessSpecialDiscounts',
				'http://tempuri.org/',
				'ProcessSpecialDiscounts',
				'http://tempuri.org/',
				'ProcessSpecialDiscountsResponse',
				'tempuriOrg.ProcessSpecialDiscountsResponse_element'}
			);
			response_x = response_map_x.get('response_x');
			return response_x.ProcessSpecialDiscountsResult;
		}

		public schemasDatacontractOrg200407PelcoSa.ProcessWarehouseInventoryDataModel ProcessWarehouseInventory(String username,String password) {
			tempuriOrg.ProcessWarehouseInventory_element request_x = new tempuriOrg.ProcessWarehouseInventory_element();
			tempuriOrg.ProcessWarehouseInventoryResponse_element response_x;
			request_x.username = username;
			request_x.password = password;
			Map<String, tempuriOrg.ProcessWarehouseInventoryResponse_element> response_map_x = new Map<String, tempuriOrg.ProcessWarehouseInventoryResponse_element>();
			response_map_x.put('response_x', response_x);
			WebServiceCallout.invoke(
				this,
				request_x,
				response_map_x,
				new String[]{endpoint_x,
				'http://tempuri.org/IQantelDataService/ProcessWarehouseInventory',
				'http://tempuri.org/',
				'ProcessWarehouseInventory',
				'http://tempuri.org/',
				'ProcessWarehouseInventoryResponse',
				'tempuriOrg.ProcessWarehouseInventoryResponse_element'}
			);
			response_x = response_map_x.get('response_x');
			return response_x.ProcessWarehouseInventoryResult;
		}
	}
}

 
Daniel BallingerDaniel Ballinger
Hi Gaurav,

This looks like you generated it using the FuseIT SFDC Explorer Wsdl2Apex tool (http://www.fuseit.com/explorer). Part of this tool includes the option to generate the Mock and associated test classes for you. It won't be an exhaustive test of the web service, but it will give you coverage of the generated code.

On the Tab where you are selecting the methods to generate code for, check the "Generate Test Classes" button in the toolbar.

User-added image

From your example code, you want something like this in your test case to call the web service.
 
@isTest
private class WebSvcCalloutTest
{
    @isTest static void testEchoString()
    {
        // This causes a fake response to be generated
        Test.setMock(WebServiceMock.class, new WebServiceTestMock());

        // Call the method that invokes a callout
        tempuriOrg.QantelDataServiceEndPoint ws = new tempuriOrg.QantelDataServiceEndPoint();
        schemasDatacontractOrg200407PelcoSa.ProcessWarehouseInventoryDataModel output = ws.ProcessWarehouseInventory('username', 'password');

        // Verify that a fake result is returned
        //System.assertEquals('Mock response', output);
    }


}



 
This was selected as the best answer
Gaurav AgnihotriGaurav Agnihotri
Thanks, Daniel.
yes, I used FuseIT to generate classes. I was able to create a test class  too and was able to get a code coverage. I will use your sample code to perform WebSvrCallout Test.

Appreciate your help.