+ Start a Discussion
sfdcFanBoysfdcFanBoy 

Create dummy response in test class for webservice callout class

Need help in writing test class for class that makes webservice callout. Only the dummy response part isn't working.
 
As suggested by other users, I have created a static variable in test class and used it in the original class also. So, while running the test class, the callout is by-passed successfully.
 
Below is the code coverage part. As you can see in the image, I am successfully by-passing the callout in Line 120.
But from Line 127 onwards, it is not covered as responseList is empty.
 
 
 
I have created the dummy response in the test class. Is this the correct way of creating the dummy response? Or do I need to make any changes in creating dummy response?
 
Here's the test class code.

Test.StartTest(); 
 
//setting the static variable to true, so it wont enter in the real webservice class
BatchKeyAccountsWS.testmode_con=true; 
 
//calling the webservice class
BatchKeyAccountsWS d = new BatchKeyAccountsWS('select id FROM Account Order by CreatedDate DESC LIMIT 1'); 
ID scheduleC = Database.executeBatch(d,1);
 
//instantiating the wsdl2apex class
nshSalesforce.SF_InPort stub = new nshSalesforce.SF_InPort(); 
 
//Creating dummy response List
List<nshSalesforce.Item_element> responseList = new List<nshSalesforce.Item_element>(); 

nshSalesforce.Item_element response = new nshSalesforce.Item_element();
response.ZTERM='1000';
response.PP='1000';
response.GUEBG=System.today();
responseList.add(response);
 
Test.StopTest();

 

 Please help
 
Arun MKArun MK

Hi,

 

Try to make use of the WebServiceMock Interface class.

 

Follow this link. It will b helpful.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

 


Regards,

Arun.

sfdcFanBoysfdcFanBoy
But mine is batch apex. Any example of test class for batch apex class with webservice callouts.