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
gwpgwp 

Help understanding unit tests

Hi, I developed the below code with the help from a sales engineer. This is my first peice of development for salesfroce so sorry if this is obvious but I've spent quite a while trying to understand the unit test concept in development.

 

It's a simple class that updates a custom object Lead_Feedback__c when a Visual Force web form submits it's result (it triggers one of three methods to do a simple update).

 

The main script ha been tested in the Sales Engineers environemtn, but when trying to move it over to our production enironment we are having trouble and the engineer doesn't know how to manage unit tests (an neither do I).

 

If you oculd help me wiht this I'm sure I can use the same approach for a trigger and VF page we've created as part of the solution:

 

 

/* Manage Partner Lead APEX Class */ public with sharing class ManagePartnerLeadClass { public final Lead l; Lead_Feedback__c lf; public string comment {get; set;} private ApexPages.StandardController lController; public ManagePartnerLeadClass(ApexPages.StandardController controller) { lController = controller; this.l = (Lead)controller.getRecord(); } //Instead of using the hard copy text you should be able to look up the value of the button selected by the user //But for the proof of concept this is fine public PageReference CreateLeadNeedMoreInfo() { try { lf = new Lead_Feedback__c(); lf.LeadID__c = l.ID; lf.Lead_Approval_Status__c = 'Need More Info'; lf.Comments__c = comment; insert lf; } catch(DmlException ex) { ApexPages.addMessages(ex); } return new PageReference('http://www.salesforcewebform.com/thankyou.html'); } public PageReference CreateLeadApproval() { try { lf = new Lead_Feedback__c(); lf.LeadID__c = l.ID; lf.Lead_Approval_Status__c = 'Approved'; lf.Comments__c = comment; insert lf; } catch(DmlException ex) { ApexPages.addMessages(ex); } return new PageReference('http://www.salesforcewebform.com/thankyou.html'); } public PageReference CreateLeadRejection() { try { lf = new Lead_Feedback__c(); lf.LeadID__c = l.ID; lf.Lead_Approval_Status__c = 'Rejected'; lf.Comments__c = comment; insert lf; } catch(DmlException ex) { ApexPages.addMessages(ex); } return new PageReference('http://www.salesforcewebform.com/thankyou.html'); } // Test method to bring this class's test coverage over the required 75% static testMethod void testLeadFeedback() { //Set context //Positive tests String comment = 'test comment'; Lead l = new Lead( Email='test@gwp.org', LastName='lead test', Company='lead test' ); insert l;// add a known lead //Negative tests CreateLeadApproval(); CreateLeadRejection(); CreateLeadNeedMoreInfo(); } }

 The tests above are obviously incomplete, but I can't get my head around the context here so I can't even get this to be accepted, syntax issues and all. I'm sure it's obvious to someone who's more experienced...Any suggestions on how I can complete it? The main body works as a proof of concept, it's just the tests...

 

Thanks!

 

 

 

 

mtbclimbermtbclimber

I don't know how this code is compiling in any environment. Those three methods under the //Negative tests comment are instance level methods, not statics.

 

Here's a basic example for one of your conditions:

 

 

/*
Manage Partner Lead APEX Class
*/
public with sharing class ManagePartnerLeadClass {

public final Lead l;
Lead_Feedback__c lf;
public string comment {get; set;}

private ApexPages.StandardController lController;

public ManagePartnerLeadClass(ApexPages.StandardController controller) {
lController = controller;
this.l = (Lead)controller.getRecord();
}

public PageReference CreateLeadApproval() {

try {
lf = new Lead_Feedback__c();
lf.LeadID__c = l.ID;
lf.Lead_Approval_Status__c = 'Approved';
lf.Comments__c = comment;
insert lf;

} catch(DmlException ex) {
ApexPages.addMessages(ex);
}

return new PageReference('http://www.salesforcewebform.com/thankyou.html');
}

/* A test that invokes the CreateLeadApproval Method and asserts
the results are successful.
*/
static testMethod void createLeadApprovalPositiveTest() {
/* Create the lead */
Lead l = new Lead( Email='test@gwp.org', LastName='lead test', Company='lead test' );
insert l;

/* Instantiate the standard controller with the lead */
ApexPages.StandardController con = new ApexPages.StandardController(l);

/* Instantiate the extention instance with the standardcontroller */
ManagePartnerLeadClass ext = new ManagePartnerLeadClass(con);

/* Set the comment on the extension */
ext.comment = 'test comment';

/* Invoke the primary method on the extension */
ApexPages.PageReference result = ext.createLeadApproval();

/* If the method failed for any reason, the catch block is
adding the exception message to the ApexPages message context.
To assert that there was no exception in this case, make sure
there are no error messages in the context */
System.assert(!ApexPages.hasMessages(ApexPages.Severity.ERROR),'An exception was thrown by the insert and messages caught.');

/* Retrieve the lead feedback from the DB that should have been created as a result
of the method invocation. */
Lead_Feedback__c lf = [select lead_approval_status__c from lead_feedback__c where leadId__c = :ext.l.id];

/* Now assert the value. */
System.assertEquals('Approved', lf.lead_approval_status__c,'Woops, the status on the lead was not as expected. ');

/* Finally, assert the resulting page */
System.assertEquals('http://www.salesforcewebform.com/thankyou.html',result.getUrl(),'URL for the Pagereference returned from createLeadApproval not as expected.');
}
}

 

 Please understand that the point of testing is not to achieve code coverage, it is to assure that your code is behaving as you expect it to.  Coverage is meant to help you identify use cases and behavior that haven't been accounted for in your tests.