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
dbrunsdbruns 

Test Method for Public String

Hello everyone,

 

 

I am very green to writing APEX and am hoping that someone might be willing to lend some advice. I have created a trigger that updates a Contact with queried information from Leads and Opportunities:

 

trigger PartnerPerformance on Contact bulk(before insert, before update){ Contact myContact = trigger.new[0]; public String getL() { if (l == null) l = 0; return '' + l; } integer l = [SELECT count() FROM Lead WHERE (lead.createddate = LAST_N_DAYS:180) AND (lead.Referring_Contact__c = :myContact.Id) LIMIT 100]; public String getQ() { if (q == null) q = 0; return '' + q; } integer q = [SELECT count() FROM Opportunity WHERE (opportunity.createddate = LAST_N_DAYS:180) AND (opportunity.Referring_Contact__c = :myContact.Id) LIMIT 1]; public String getC() { if (c == null) c = 0; return '' + c; } integer c = [SELECT count() FROM Opportunity WHERE (opportunity.closedate = LAST_N_DAYS:180) AND (opportunity.stagename = 'Closed Won') AND (opportunity.Referring_Contact__c = :myContact.Id) LIMIT 100]; if (myContact.Id != null) { myContact.Referred_Leads__c = l; myContact.Qualified_Leads__c = q; myContact.Closed_Won_Opps__c = c; } }

 

The challenge is that my Test Code only covers 40% and disregards all of my Public Strings. Can anyone provide an example of how to test the Public String established in the trigger? Thank you!

 

@istest private class PastPerformanceTriggerTest { static testMethod void canInsertPerformance(){ Account account = TestUtilities.setupAccountTestData(); Account partneraccount= new Account(Name = 'TestPartnerAccount', Phone = '(000) 000-0000'); insert partneraccount; Contact contact = TestUtilities.setupContactTestData(); Contact partner = new Contact(FirstName = 'Test', LastName = 'Partner', Partner_Tier__c= 'Standard', Phone = '(000) 000-0000', Email = 'unk@guidantfinancial.com', AccountID = partneraccount.ID); test.startTest(); insert partner; if(partner.Referred_Leads__c == 0){ System.Assert(false); } else{ System.Assert(true); } if(partner.Qualified_Leads__c == 0){ System.Assert(false); } else{ System.Assert(true); } if(partner.Closed_Won_Opps__c == 0){ System.Assert(false); } else{ System.Assert(true); } test.stopTest(); } static testMethod void canUpdatePerformance(){ Account account = TestUtilities.setupAccountTestData(); Account partneraccount= new Account(Name = 'TestPartnerAccount', Phone = '(000) 000-0000'); insert partneraccount; Contact contact = TestUtilities.setupContactTestData(); Contact partner = new Contact(FirstName = 'Test', LastName = 'Partner', Partner_Tier__c= 'Standard', Phone = '(000) 000-0000', Email = 'unk@guidantfinancial.com', AccountID = partneraccount.ID); insert partner; Lead lead = TestUtilities.setupLeadTestData(); Lead partnerlead= new Lead(FirstName = 'Test', LastName = 'One', Company = 'Test Company', Status = 'Outreach', Phone = '(000) 000-0000', Email = 'unk@guidantfinancial.com', ProductInterest__c = '401k Small Business Financing', Referring_Contact__c = partner.Id); Lead nonpartnerlead= new Lead(FirstName = 'Test', LastName = 'Two', Company = 'Test Company', Status = 'Outreach', Phone = '(000) 000-0000', Email = 'unk@guidantfinancial.com', ProductInterest__c = '401k Small Business Financing'); insert partnerlead; insert nonpartnerlead; Opportunity opportunity = TestUtilities.setupOpportunityTestData(); Opportunity opportunity1 = new Opportunity(Name = 'Test Partner Opportunity', AccountID = partneraccount.Id, CloseDate = date.today(), StageName = 'Closed Won', Areas_of_Interest__c = 'Business: Existing', Product_Interest__c = '401k Small Business Financing'); insert opportunity1; test.startTest(); update partner; if(partner.Referred_Leads__c == 1){ System.Assert(false); } else{ System.Assert(true); } if(partner.Qualified_Leads__c == 1){ System.Assert(false); } else{ System.Assert(true); } if(partner.Closed_Won_Opps__c == 1){ System.Assert(false); } else{ System.Assert(true); } test.stopTest(); } }

 

Message Edited by dbruns on 12-21-2009 11:43 PM
Best Answer chosen by Admin (Salesforce Developers) 
rocwilcoxrocwilcox

Your real problem it seems is not in your testing, but rather that your trigger does not use the public string funtions that you declared.  1st are those fields on Contact actually string fields? if so then Change these to

myContact.Referred_Leads__c = getL();
myContact.Qualified_Leads__c = getQ();
myContact.Closed_Won_Opps__c = getC();

 If they are not strings, then redeclare them in the appropriate data type to match....

All Answers

rocwilcoxrocwilcox

Your real problem it seems is not in your testing, but rather that your trigger does not use the public string funtions that you declared.  1st are those fields on Contact actually string fields? if so then Change these to

myContact.Referred_Leads__c = getL();
myContact.Qualified_Leads__c = getQ();
myContact.Closed_Won_Opps__c = getC();

 If they are not strings, then redeclare them in the appropriate data type to match....

This was selected as the best answer
dbrunsdbruns
Thank you! I knew I was overlooking something obvious.