You need to sign in to do that
Don't have an account?
First Test Class Suggestions
I have written my first class, class test and trigger. I am currently working on the test code for the trigger, but in the meantime I was wondering if anyone out there would give me some pointers on what I have already created...
The class extension code is below, followed by the test code that I wrote...
public class myWeb2TransExtension { private final Transmittal_WebForm__c webtrans; public myWeb2TransExtension(ApexPages.StandardController stdController) { webtrans = (Transmittal_WebForm__c)stdController.getRecord(); } public PageReference saveTransmittal_WebForm() { try { insert(webtrans); } catch(System.DMLException e) { ApexPages.addMessages(e); return null; } PageReference p = Page.TransmittalThankYou; p.setRedirect(true); return p; } }
@isTest private class Web2TransTests { static testMethod void saveWebTransOK() { // Populate Transmittal WebForm Transmittal_WebForm__c t = new Transmittal_WebForm__c( MSS_CAA_Name__c='testmss', Lead_Source__c='Current', County_Office__c='testcounty', Contract_Number_SSN__c='000000000', Farm_Bureau_Member_Number__c='123456', First_Name__c='testfirst', Street_Address__c='testaddress', Middle_Initial__c='testmiddle', City__c='testcity', Last_Name__c='testlast', Zip_Postal_Code__c='55555', Email_Address__c='testemail', Phone_Number__c='3333334444', Type_of_Transaction__c='new', Product_Type__c='PHC', Product_Subtype__c='Premier'); test.startTest(); // Instantiate a Controller Extension myWeb2TransExtension ext = new myWeb2TransExtension(new ApexPages.StandardController(t)); // Call the saveTransmittal_WebForm method PageReference pr = ext.saveTransmittal_WebForm(); test.stopTest(); // Make sure that everything works correctly and redirects to Thank You Page system.assertEquals(Page.TransmittalThankYou.getUrl(),pr.getUrl()); } static testMethod void saveWebTransKO() { // Populate Transmittal WebForm Transmittal_WebForm__c t = new Transmittal_WebForm__c( Lead_Source__c='Current Customer', County_Office__c='testcounty2', Contract_Number_SSN__c='333224444', Farm_Bureau_Member_Number__c='234567', First_Name__c='testfirst2', Street_Address__c='testaddress2', Middle_Initial__c='testmiddle2', City__c='testcity2', Last_Name__c='testlast2', Zip_Postal_Code__c='77777', Email_Address__c='testemail@2.com', Phone_Number__c='2223334444', Type_of_Transaction__c='new', Product_Type__c='PHC', Product_Subtype__c='Coreshare'); test.startTest(); // Instantiate a Controller Extension myWeb2TransExtension ext = new myWeb2TransExtension(new ApexPages.StandardController(t)); // Call the saveTransmittal_WebForm method PageReference pr = ext.saveTransmittal_WebForm(); test.stopTest(); // Make sure that everything works correctly and redirects to Thank You Page system.assertEquals(null,pr); } }
It is a simple class extension and test code that I was, thankfully, about to figure out with the help of this discussion board. Even though I have been able to get the required 75% code coverage with this test code, I was wondering if I could get some pointers/best practices on how to accomplish 100% code coverage and/or better ways to write the code itself.
Thanks!
Hi,
You can use following 2 links for writing Test Methods
http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods
http://wiki.developerforce.com/index.php/Apex_Code_Best_Practices
Hope this will help you!!