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
sldfkjsldfkj 

0% Test Coverage - Apex Code Generated by WSDL

I have an external web service that's called from a class generated from a wsdl file.  I've wrapped the invoke method with a Test.isRunningTest() check so that my tests don't fail due to a callout.  My assert comes back successful, but code coverage remains at 0%.

 

(code posted below)

 

Any thoughts and/or ideas would be greatly appreciated.

 

Thank you in advance!

Sam

 

APEX Test:

@isTest

private Class MyService

{

    publicstatictestMethodvoid TestMyService()

    {

        try

        {

            MyService.AccountServiceSoap cs = new MyService.AccountServiceSoap();

            DateTime dt = cs.GetFirstInstallAppointmentAvailable('token', '', '', '', '', '10023', 982);

            

            //With the callout, I know that the returned result will be 1/1/1900

            DateTime expected = DateTime.newInstanceGMT(1900,1,1);

            

            System.Debug('ClickServiceTest Pre dt:' + dt + ' / expected:' + expected);

            

            System.AssertEquals(dt,expected);

            

            System.Debug('ClickServiceTest Post dt:' + dt + ' / expected:' + expected);

        }

        catch(Exception ex)

        {

            System.Debug('Error: ' + ex.getMessage());

        }

    }

}

 

 

APEX Class:

//Generated by wsdl2apex

public class MyService {
public class Available_element {
public String token;
public String address1;
public String address2;
public String city;
public String stateAbbrev;
public String postalCode;
public Integer serviceCodeID;
private String[] token_type_info = new String[]{'token','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] address1_type_info = new String[]{'address1','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] address2_type_info = new String[]{'address2','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] city_type_info = new String[]{'city','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] stateAbbrev_type_info = new String[]{'stateAbbrev','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] postalCode_type_info = new String[]{'postalCode','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] serviceCodeID_type_info = new String[]{'serviceCodeID','http://www.w3.org/2001/XMLSchema','int','1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
private String[] field_order_type_info = new String[]{'token','address1','address2','city','stateAbbrev','postalCode','serviceCodeID'};
}
public class AvailableResponse_element {
public DateTime GetFirstInstallAppointmentAvailableResult;
private String[] GetFirstInstallAppointmentAvailableResult_type_info = new String[]{'GetFirstInstallAppointmentAvailableResult','http://www.w3.org/2001/XMLSchema','dateTime','1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
private String[] field_order_type_info = new String[]{'GetFirstInstallAppointmentAvailableResult'};
}
public class AccountServiceSoap {
public String endpoint_x = 'http://someservice.com';
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/', 'MyService'};

public DateTime GetFirstInstallAppointmentAvailable(String token,String address1,String address2,String city,String stateAbbrev,String postalCode,Integer serviceCodeID) {
MyService.Available_element request_x = new MyService.Available_element();
MyService.AvailableResponse_element response_x;
request_x.token = token;
request_x.address1 = address1;
request_x.address2 = address2;
request_x.city = city;
request_x.stateAbbrev = stateAbbrev;
request_x.postalCode = postalCode;
request_x.serviceCodeID = serviceCodeID;
Map<String, MyService.AvailableResponse_element> response_map_x = new Map<String, MyService.AvailableResponse_element>();
response_map_x.put('response_x', response_x);

if(Test.isRunningTest())
{
return DateTime.newInstanceGMT(1900,1,1);
}

WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'http://tempuri.org/GetFirstInstallAppointmentAvailable',
'http://tempuri.org/',
'GetFirstInstallAppointmentAvailable',
'http://tempuri.org/',
'GetFirstInstallAppointmentAvailableResponse',
'MyService.AvailableResponse_element'}
);
response_x = response_map_x.get('response_x');
return response_x.GetFirstInstallAppointmentAvailableResult;
}
}
}

Starz26Starz26

Ahh, the joys of WSDL and websaervice.

 

Unfortunatly, test methods do not run webservice stuff. Basically once the call to the webservice happens, the test automatically stops.

 

Also, using RUN TEST in the test method will show NO coverage while useing apex test execution will show the actual coverage.

 

Eaisest way to cover code in WSDL I have found is to simply write a test method that instantiates ALL classes int he WSDL as most of them just set variables.

 

 

A previous response by Ron Hess to a similar question is as follows:

 

"testing your code with web service callouts will leave a few lines of code unexecuted, this is manageable by putting very little code after the callout or http.send();

instead, just exit the method after the send and breakout subsequent functionality into separate methods, these are then callable from test methods by constructing (mock) response objects and passing these into these separate methods. "

sldfkjsldfkj

Thank you for the response.

 

From suggestion 1 I changed:

if(Test.isRunningTest())

{

    return DateTime.newInstanceGMT(1900,1,1);

}

 

To:

public class AccountServiceSoap {

        public Boolean test {get;set;}

.....

public AccountServiceSoap()

        {

        test = false;

        }

.....

public DateTime GetFirstInstallAppointmentAvailable(String token,String address1,String address2,String city,String stateAbbrev,String postalCode,Integer serviceCodeID) {

.....

if(test)

           {

            return DateTime.newInstanceGMT(1900,1,1);

           }

....

}

}

 

Still no code coverage is shown. 

 

From suggestion 2:

  • I tried instantiating all the classes without calling the method.  No code coverage.
  • I tried moving the Inoke method out to its own method.  To do this I had to pass the parameters in to the newly created method or create them as global private variables to the AccountServiceSoap class.  Both gave a runtime error saying it couldn't find the request_x object even though it was global to the class or passed in as a parameter.

Any other ideas or suggestions for me to try?

 

Thank you!

Starz26Starz26

did you run the test from the apex test execution section of the setup area or from the run tests button of the class?

 

 

sldfkjsldfkj

I've done both

 

Running from the apex class "Run Test" button shows that the Assert statements pass but there is 0 code coverage in any of the called classes.

Running from the "Apex Test Execution" section under Develop shows as well that the Assert Statements pass.

 

My main issue is the code coverage staying at 0% even when the code is called and ran all the way through the asserts, which show valid.

 

Thanks!

amidstcloudamidstcloud

Facing Same issue . neone has the solution for it... 

BBeairdBBeaird

1 year later. This problem still exists.  I'm using mock webservices in my tests, which are definitely calling the generated apex classes, yet they all register as 0%.