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 writing test case for class and trigger

HI, I just cant seem to get any coverage on my trigger that is run when a custom object gets update.

A visual force page uses the following code to update a lead via a trigger attached to a custom object that the page creates.

 

I have one class and one trigger, I have put the test method in the class. This feels like the right thing to do but for the life of me (it's my first script) I cant get the test to run. The code is basic and does work in sandbox...

 

 

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'); } static testMethod void myUnitTest() { //Create Lead for the test Lead l = new Lead( Email='test@gwp.test', LastName='Test', Company='Test' ); insert l; // add a known lead //New instance of the standard page controller for this lead ApexPages.StandardController con = new ApexPages.StandardController(l); //New instance of the extension class ManagePartnerLeadClass mpl = new ManagePartnerLeadClass(con); //Set loose values mpl.comment = 'test comment'; //Test methods PageReference endPage = mpl.CreateLeadApproval(); //Confirm output System.assertEquals(endPage.getUrl(),'http://www.salesforcewebform.com/thankyou.html'); //Test feedback insert Lead_Feedback__c tlf = new Lead_Feedback__c(LeadID__c = l.ID, Lead_Approval_Status__c = 'Approved',Comments__c='Trigger test'); insert tlf; //Test to see if trigger was hit String leadId = tlf.LeadID__c; Lead trl = [select Id from Lead where Id =:leadId]; System.assertEquals(leadId, trl.Id); } }

 The trigger

 

trigger Update_Lead_Feedback on Lead_Feedback__c (after insert) { //also , after update required? // trigger automatically syncs Lead Approval to the Lead record. Lead l; for (Lead_Feedback__c lf : Trigger.new) { String leadId = lf.LeadID__c; if (leadId !=null && leadId !='') { l = [select Id from Lead where Id=:leadId]; l.Lead_Approval__c = lf.Lead_Approval_Status__c; l.Lead_Approval_Feedback__c = lf.Comments__c; update l; } } }

 

 

 

 Big thanks, I'm sitting on a risky project after having toted Salesforce as THE platform we should be using... now I'm beginning to worry as development is down to a snails pace, any suggestions to improve the code? Perhaps it's my whole approach???

 

 

Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber

Did you see my response to your other post?

 

http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=23413#M23413

 

It's always good to spin up new threads for new issues but this seems like it's a continuation of your previous one which is still "unresolved" and should have been posted there as a response to mine.

 

That being said you have a number of issues here:

 

1) The code posted above is inefficient and will hit limits when it operates in bulk.  Remedy: Remove the query and dml operation on Lead from the loop. The processing should be: gather unique lead IDs to query, query once in bulk and then update in bulk.   And what should happen when there are two competing lead_feedback__c records in the same request? Which status "wins"?

 

2) Why are you calling insert again when your method call on the controller extension performs an insert? If you want to test the insert operation explicitly, I recommend a second test and would have you look into asserting the bulk case.

 

Can you describe the issue you are hitting in more detail? Are you saying your unit test is failing in production but not sandbox? If so what is the failure message?   Just by eye-balling it I can't see why your trigger wouldn't be executing as long as it's active and in fact it should be executed twice with the current code.

 

 

 

 

 

Message Edited by mtbclimber on 12-07-2009 02:40 PM

All Answers

mtbclimbermtbclimber

Did you see my response to your other post?

 

http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=23413#M23413

 

It's always good to spin up new threads for new issues but this seems like it's a continuation of your previous one which is still "unresolved" and should have been posted there as a response to mine.

 

That being said you have a number of issues here:

 

1) The code posted above is inefficient and will hit limits when it operates in bulk.  Remedy: Remove the query and dml operation on Lead from the loop. The processing should be: gather unique lead IDs to query, query once in bulk and then update in bulk.   And what should happen when there are two competing lead_feedback__c records in the same request? Which status "wins"?

 

2) Why are you calling insert again when your method call on the controller extension performs an insert? If you want to test the insert operation explicitly, I recommend a second test and would have you look into asserting the bulk case.

 

Can you describe the issue you are hitting in more detail? Are you saying your unit test is failing in production but not sandbox? If so what is the failure message?   Just by eye-balling it I can't see why your trigger wouldn't be executing as long as it's active and in fact it should be executed twice with the current code.

 

 

 

 

 

Message Edited by mtbclimber on 12-07-2009 02:40 PM
This was selected as the best answer
MonishMonish

Hi All

 

I am new to salesforce.I am having a doubt like how salesforce app is going to be tested?what are all the possible ways?

I know developer can write the test methods for triggers and apex controllers etc.But what a salesforce tester do?

Can any body explain?