You need to sign in to do that
Don't have an account?
sfdc wizard
Advice on writing a Test Class
Hello All,
So far I've had experience writing Apex Triggers & the Test Classes. I am looking for some pointers that will help me modify the test class, which will give me a better coverage of the Apex Class. I've always written Test classes separately, and not within the class. It was written by our previous developer that is a part of the syncing (salesforce & remedy).
I am open to either modify the existing class/test class or just write a separate test class that will give me a better code coverage( Current 29%). The lines not covered by the test class are highlighted in Bold, Italic.
Below is the complete Class and the Test class within:
global class WS_RemedyEW{
WebService static list<list<String>> getRemedyEW(String serialNumber, String SONumber){
fxExtendedWarranty.FX_Extended_WarrantySoap syncRem = new fxExtendedWarranty.FX_Extended_WarrantySoap();
fxExtendedWarranty.AuthenticationInfo syncRemAuth = new fxExtendedWarranty.AuthenticationInfo();
syncRemAuth.userName = 'mthomas';
syncRemAuth.password= 'tinu@1980';
syncRem.parameters = syncRemAuth;
try{
fxExtendedWarranty.getListValues_element[] getResponce = syncRem.OpGetList('','','','','','','','','','','','','','','','','',serialNumber,SONumber,'');
list<list<string>> ewDetails = new list<list<string>>();
for(fxExtendedWarranty.getListValues_element ewItem: getResponce){
list<string> ewElement = new list<string>();
ewElement.add(ewItem.Serial_Number);
ewElement.add(ewItem.SO_Number);
ewElement.add(ewItem.CommencementMonth);
ewElement.add(ewItem.CommencementYear);
ewElement.add(ewItem.Company_Name);
ewElement.add(ewItem.Customer_Company_Name);
ewElement.add(ewItem.CustomerName);
ewElement.add(ewItem.Dealer);
ewElement.add(ewItem.ExpirationMonth);
ewElement.add(ewItem.ExpirationYear);
ewElement.add(ewItem.Full_EW);
ewElement.add(ewItem.HW_EW);
ewElement.add(ewItem.Part_Number);
ewElement.add(ewItem.PONumber);
ewElement.add(ewItem.Product);
ewElement.add(ewItem.ProductName);
ewElement.add(ewItem.SW_FW_EW);
ewElement.add(ewItem.ADV_EW);
if (string.valueof(ewItem.POS_Date).length() > 0) {ewElement.add(ewItem.POS_Date.substring(0,10));} else {ewElement.add(ewItem.POS_Date);}
ewDetails.add(ewElement);
}
return ewDetails;
}catch(Exception e){
//Write to the error log file
}
return null;
}
private static testMethod void myTest() {
getRemedyEW('MSN%','1');
}
}
So far I've had experience writing Apex Triggers & the Test Classes. I am looking for some pointers that will help me modify the test class, which will give me a better coverage of the Apex Class. I've always written Test classes separately, and not within the class. It was written by our previous developer that is a part of the syncing (salesforce & remedy).
I am open to either modify the existing class/test class or just write a separate test class that will give me a better code coverage( Current 29%). The lines not covered by the test class are highlighted in Bold, Italic.
Below is the complete Class and the Test class within:
global class WS_RemedyEW{
WebService static list<list<String>> getRemedyEW(String serialNumber, String SONumber){
fxExtendedWarranty.FX_Extended_WarrantySoap syncRem = new fxExtendedWarranty.FX_Extended_WarrantySoap();
fxExtendedWarranty.AuthenticationInfo syncRemAuth = new fxExtendedWarranty.AuthenticationInfo();
syncRemAuth.userName = 'mthomas';
syncRemAuth.password= 'tinu@1980';
syncRem.parameters = syncRemAuth;
try{
fxExtendedWarranty.getListValues_element[] getResponce = syncRem.OpGetList('','','','','','','','','','','','','','','','','',serialNumber,SONumber,'');
list<list<string>> ewDetails = new list<list<string>>();
for(fxExtendedWarranty.getListValues_element ewItem: getResponce){
list<string> ewElement = new list<string>();
ewElement.add(ewItem.Serial_Number);
ewElement.add(ewItem.SO_Number);
ewElement.add(ewItem.CommencementMonth);
ewElement.add(ewItem.CommencementYear);
ewElement.add(ewItem.Company_Name);
ewElement.add(ewItem.Customer_Company_Name);
ewElement.add(ewItem.CustomerName);
ewElement.add(ewItem.Dealer);
ewElement.add(ewItem.ExpirationMonth);
ewElement.add(ewItem.ExpirationYear);
ewElement.add(ewItem.Full_EW);
ewElement.add(ewItem.HW_EW);
ewElement.add(ewItem.Part_Number);
ewElement.add(ewItem.PONumber);
ewElement.add(ewItem.Product);
ewElement.add(ewItem.ProductName);
ewElement.add(ewItem.SW_FW_EW);
ewElement.add(ewItem.ADV_EW);
if (string.valueof(ewItem.POS_Date).length() > 0) {ewElement.add(ewItem.POS_Date.substring(0,10));} else {ewElement.add(ewItem.POS_Date);}
ewDetails.add(ewElement);
}
return ewDetails;
}catch(Exception e){
//Write to the error log file
}
return null;
}
private static testMethod void myTest() {
getRemedyEW('MSN%','1');
}
}
Option 1:
Please skip the call of service in webservice class and use the dummy data of response of callouts using Test.istestRunning().
Test class cannot be used to test Web service callout. The Test.isRunningTest() method is a useful method to bypass web service callouts when running automated test cases that would otherwise fail.
For example, in any wsdl2apex generated classes find the WebServiceCallout.invoke() method calls and then wrap them in an test for Test.isRunningTest(). If the test is false the code can still call invoke as generated. Otherwise the code can simulate the web service response, allowing the test cases to complete.
http://www.eltoro.it/ArticleViewer?id=a07A000000NPRjNIAX
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm
http://boards.developerforce.com/t5/Apex-Code-Development/how-to-write-test-class-for-webservice-method/m-p/625245#M115307
http://blog.wdcigroup.net/2012/08/salesforce-web-service-callout-test-class/
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Option 2:
Test Web Service Callouts
Generated code is saved as an Apex class containing the methods you can invoke for calling the web service. To deploy or package this Apex class and other accompanying code, 75% of the code must have test coverage, including the methods in the generated class. By default, test methods don’t support web service callouts, and tests that perform web service callouts fail. To prevent tests from failing and to increase code coverage, Apex provides the built-in WebServiceMock interface and the Test.setMock method. Use WebServiceMock and Test.setMock to receive fake responses in a test method.
Specify a Mock Response for Testing Web Service Callouts
When you create an Apex class from a WSDL, the methods in the auto-generated class call WebServiceCallout.invoke, which performs the callout to the external service. When testing these methods, you can instruct the Apex runtime to generate a fake response whenever WebServiceCallout.invoke is called. To do so, implement the WebServiceMock interface and specify a fake response for the Apex runtime to send. Here are the steps in more detail.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
---------------------------------------------------------------------------------------------------------------------
Also follow the below Links for on similar post:
https://developer.salesforce.com/forums/?id=906F000000094EyIAI
https://developer.salesforce.com/forums/?id=906F0000000BVqxIAG
https://developer.salesforce.com/forums/?id=906F0000000AhBYIA0
https://developer.salesforce.com/forums/?id=906F000000093EuIAI
https://developer.salesforce.com/forums/?id=906F0000000927CIAQ
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
http://www.greytrix.com/blogs/salesforce/2015/02/02/testing-web-service-callouts/
http://salesforce-evershine-knowledge.blogspot.com/2012/10/testing-web-service-callouts.html
Please do let me know if it helps you.
Regards,
Mahesh