+ Start a Discussion
sfdcFanBoysfdcFanBoy 

Test method for batch class with webservice callouts

I have a batch class with callouts made to external webservice in the execute method.  A schedular class passes records to this batch class at batch size of 1.

 

I am trying to write test method only for batch class.  How do I call webservice methods inside the execute method?  I used database.executeBatch method in test method, it covers part the execute method.. but stops at the line where the  webservice call is made.

 

This is the webservice call made in the execute method.  Passing the SoldCode to webservice and processing the response.  The 2nd line is not covered in the code coverage part as I am not sure how to call this in the test method.

 

SalesforceCom_Live.SFDSPort stub_DS = new SalesforceCom_Live.SFDSPort();
SalesforceCom_Live.SFResponse_SFDS response_difot = stub_DS.SFDS(SoldCode);

 SalesforceCom_Live is the Apex2WSDL Class.

 SFDSPort is the method that has the endpoint, request and response elements.

 SFResponse_SFDS is the response method in it.

 

In the test method, I have started with this.

Batch_DIFOT d = new Batch_DIFOT('select id .........);
ID scheduleDIFOT = Database.executeBatch(d,1);

 

How do I call the 2nd line to proceed ? 

 

help ?

 

Thanks.

vbsvbs
Manish - You will have to create a mock web service similiar to the one listed here:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm
You will then have to modify the code in the batch apex to check if the test is running then call the mock webservice else call the regular webservice mentioned by you.
sfdcFanBoysfdcFanBoy

The problem is I do not have a separate method for the Webservice callout in the batch class.  The callouts are done in the Execute method itself. 

 

So, as shown in the example, I cannot call the method explicitly.

 

WebSvcCallout.callEchoString('Hello World!');

vbsvbs

Thats what I stated. You will need to create a new Mock Class with the implementation specified in the link above. Then try this:

if (!Test.isRunningTest()) {
    SalesforceCom_Live.SFResponse_SFDS response_difot = stub_DS.SFDS(SoldCode);
} else {
    SalesforceCom_Live.SFResponse_SFDS response_difot = mockTestSub.SFDS(SoldCode);
}

 

sfdcFanBoysfdcFanBoy

I have added the code as per your suggestion, but in my mockwebservice i do not have the SFDS as a separate method hence it is giving error.

 

SalesforceCom_Live.SFResponse_SFDS response_difot = WebServiceMockImpl_DSWS.SFDS(SoldCode);

 

SFDS method doesn't exists

 

This is the webservice mock class I created before.

 

@isTest
global class WebServiceMockImpl_DSWS 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) {
       SalesforceCom_Live.SFResponse_SFDS respElement = new SalesforceCom_Live.SFResponse_SFDS();
       respElement.CREQ = 1000;       
       respElement.PROM = 1000;        
       respElement.UORD = 10;        
       respElement.RLOAD = '10';        
       respElement.DSO = 100;        
       respElement.CONVOL = 100;
       response.put('response_x', respElement); 
   }
}

 

This is where I have been struck before too.  Thanks for your patience.

vbsvbs

Did you add this in the test class. This should call your test mock callout instead of the actual one:

Test.setMock(WebServiceMock.class, new WebServiceMockImpl_DSWS());
sfdcFanBoysfdcFanBoy
Yes. I did add. The error is in the actual apex class when writing the isTestRunning else part


SalesforceCom_Live.SFResponse_SFDS response_difot = WebServiceMockImpl_DSWS.SFDS(SoldCode);

The mock class doesnt have SFDS method.
sfdcFanBoysfdcFanBoy
Any suggestion how to proceed please?

Thanks!