You need to sign in to do that
Don't have an account?
sforcegarrett7
Unit Testing Controller Extension
Hi all,
With the help of my mentor/boss we...mostly he...created the following Apex Class to return the most recent related list record for a Contact:
//======================================================== // Name: GenerateTopListResults // Type: Class // Purpose: This class is used as an Visualforce page extension to retrieve Contact related list items // Created by: Garrett Farwell and Don Robins // Created on: Aug 9, 2013 // // Rev # Revised on Revised by Revision Description // ----- ---------- ------------------------------------- // 1.0 8/9/2013 Garrett and Don Initial Release //========================================================= public class GenerateTopListResults { //attribute of the class //needs to be private since we are only using it in this class private ID currentContactId; //Constructor to get the Contact Id from the Controller Extension public GenerateTopListResults(ApexPages.StandardController stdController) { currentContactId = stdController.getId(); } //A getter method for each list is utilized //Campaign Members Volunteer List public List<CampaignMember> getCampaignMemberVolunteerList() { Return [SELECT Id, Campaign.Name, Campaign.StartDate, Campaign.RecordTypeId FROM CampaignMember WHERE Campaign.RecordTypeID = '012U00000001486' AND ContactId = : currentContactId ORDER BY Campaign.StartDate DESC Limit 1]; } //Campaign Members Communication List public List<CampaignMember> getCampaignMemberCommunicationList() { Return [SELECT Id, Campaign.Name, Campaign.StartDate, Campaign.RecordTypeId FROM CampaignMember WHERE Campaign.RecordTypeID = '012U000000014Bo' AND ContactId = : currentContactId ORDER BY Campaign.StartDate DESC Limit 1]; } //RTP Relationship List public List<Relationship__c> getRelationshipList() { Return [SELECT Start_Date__c, Interest__c,Program_Interest__c,Role__c FROM Relationship__c WHERE Contact__c = : currentContactId ORDER BY Start_Date__c DESC Limit 1]; } //Documentation List public List<Documentation__c> getDocumentationList() { Return [SELECT Contacts_Documentation__c, Number_of_Applications__c FROM Documentation__c WHERE Contacts_Documentation__c = : currentContactId]; } //Donation List public List<OpportunityContactRole> getDonationList() { Return [SELECT ContactId, Role, Opportunity.Name, Opportunity.CloseDate FROM OpportunityContactRole WHERE Role = 'Donor' AND ContactId = : currentContactId AND Opportunity.RecordTypeID <> '012U000000014Wp' AND Opportunity.IsWon = true ORDER BY Opportunity.CloseDate DESC Limit 1]; } }
I am using this class as an extension on a visualforce page. It is just to display the results of the quieries on a Contact record. I am new to this stuff...how would I write a Test Class to cover the above code?
I started to set up the test data but I am not sure if I am doing this correctly. Even once I have the test data how do I test it properly? I really appreciate anyone's help!
//======================================================== // Name: GenerateTopListResultsTest // Type: Test Class // Purpose: Tests for the GetTopListResultsClass // Created by: Garrett Farwell // Created on: August 9th, 2013 // // Rev # Revised on Revised by Revision Description // ----- ---------- ------------------------------------- // 1.0 05/10/2013 Garrett Farwell Initial Release //========================================================= @isTest private class GenerateTopListResultsTest { static testMethod void canInsertTestRecords() { //============================================================= //SETUP //============================================================= //Create a mock Contact record Contact a = new Contact(LastName='Test Contact'); INSERT a; //Create mock Campaign records Campaign c1 = new Campaign(Name='TestCommunicationCampaign1', StateDate = '2013-08-09', IsActive = 'true', RecordTypeId = '012U000000014Bo'); Campaign c2 = new Campaign(Name='TestVolunteerCampaign2', StateDate = '2013-02-09', IsActive = 'true', RecordTypeId = '012U00000001486'); Campaign c3 = new Campaign(Name='TestCommunicationCampaign3', StateDate = '2013-04-09', IsActive = 'true', RecordTypeId = '012U000000014Bo'); Campaign c4 = new Campaign(Name='TestVolunteerCampaign4', StateDate = '2013-05-09', IsActive = 'true', RecordTypeId = '012U00000001486'); List<Campaign> campaignList = new List<Campaign> { c1, c2, c3, c4 }; INSERT campaignList; //Create mock CampaignMember records CampaignMember cm1 = new CampaignMember(CampaignId=c1.Id, ContactId=a.Id); CampaignMember cm2 = new CampaignMember(CampaignId=c2.Id, ContactId=a.Id); CampaignMember cm3 = new CampaignMember(CampaignId=c3.Id, ContactId=a.Id); CampaignMember cm4 = new CampaignMember(CampaignId=c4.Id, ContactId=a.Id); List<CampaignMember> campaignMemberList = new List<CampaignMember> { cm1, cm2, cm3, cm4 }; INSERT campaignMemberList; //Create mock RTP Relationship records Relationship__c r1 = new Relationship__c(Contact__c=a.Id, State_Date__c = '2013-06-18'); Relationship__c r2 = new Relationship__c(Contact__c=a.Id, State_Date__c = '2013-07-15'); Relationship__c r3 = new Relationship__c(Contact__c=a.Id, State_Date__c = '2013-08-21'); Relationship__c r4 = new Relationship__c(Contact__c=a.Id, State_Date__c = '2013-02-10'); List<Relationship__c> rtpRelationshipList = new List<Relationship__c> { r1, r2, r3, r4 }; INSERT rtpRelationshipList; //Create a mock Documentation record Documentation__c d1 = new Documentation__c(Contact__c=a.Id) INSERT d1; //Create Repair Application records Repair_Application__c ra1 = new Repair_Application__c(Documentation_Reference__c = d1.Id); Repair_Application__c ra2 = new Repair_Application__c(Documentation_Reference__c = d1.Id); List<Repair_Application__c> repairApplicationList = new List<Repair_Application__c> { ra1, ra2 }; INSERT repairApplicationList; //Create Donation/Opportunity records Opportunity o1 = new Opportunity(Name= 'TestOpp1', CloseDate = '2013-06-07'); Opportunity o2 = new Opportunity(Name= 'TestOpp2', CloseDate = '2013-07-15'); Opportunity o3 = new Opportunity(Name= 'TestOpp3', CloseDate = '2013-08-07'); List<Opportunity> opportunityList = new List<Opportunity> { o1, o2, o3 }; INSERT opportunityList; //Create ContactRole entries OpportunityContactRole ocr1 = new OpportunityContactRole(ContactId = a.Id, OpportunityId = o1.Id, Role = 'Donor'); OpportunityContactRole ocr2 = new OpportunityContactRole(ContactId = a.Id, OpportunityId = o2.Id, Role = 'Donor'); OpportunityContactRole ocr3 = new OpportunityContactRole(ContactId = a.Id, OpportunityId = o3.Id, Role = 'Donor'); List<OpportunityContactRole> opportunityContactRoleList = new List<OpportunityContactRole> { ocr1, ocr2, ocr3 }; INSERT opportunityContactRoleList; householdIds.add(h.id); //Create some mock Opportunities for the household. Opportunity o1 = new Opportunity(name='Donation1', StageName = 'Posted', Amount = 500, Household__c = h.id, CloseDate = date.today(), AccountId = a.Id ); Opportunity o2 = new Opportunity(name='Donation2', StageName = 'Posted', Amount = 750, Household__c = h.id, CloseDate = date.today(), AccountId = a.Id ); Opportunity o3 = new Opportunity(name='Donation3', StageName = 'Posted', Amount = 250, Household__c = h.id, CloseDate = date.today(), AccountId = a.Id );
k_bensten...you rock! Thank you for your help with this. I think I am all clear here! This is my final product!
All Answers
Add the below to your test class to mimic the behavior of the VF page calling the getter methods to return related records:
Replace ...etc with calls to each of the getter methods.
Try like this
@isTest
private class GenerateTopListResultsTest {
static testMethod void canInsertTestRecords() {
//=============================================================
//SETUP
//=============================================================
//Create a mock Contact record
Contact a = new Contact(LastName='Test Contact');
INSERT a;
//Create mock Campaign records
Campaign c1 = new Campaign(Name='TestCommunicationCampaign1', StateDate = '2013-08-09', IsActive = 'true', RecordTypeId = '012U000000014Bo');
Campaign c2 = new Campaign(Name='TestVolunteerCampaign2', StateDate = '2013-02-09', IsActive = 'true', RecordTypeId = '012U00000001486');
Campaign c3 = new Campaign(Name='TestCommunicationCampaign3', StateDate = '2013-04-09', IsActive = 'true', RecordTypeId = '012U000000014Bo');
Campaign c4 = new Campaign(Name='TestVolunteerCampaign4', StateDate = '2013-05-09', IsActive = 'true', RecordTypeId = '012U00000001486');
List<Campaign> campaignList = new List<Campaign> { c1, c2, c3, c4 };
INSERT campaignList;
//Create mock CampaignMember records
CampaignMember cm1 = new CampaignMember(CampaignId=c1.Id, ContactId=a.Id);
CampaignMember cm2 = new CampaignMember(CampaignId=c2.Id, ContactId=a.Id);
CampaignMember cm3 = new CampaignMember(CampaignId=c3.Id, ContactId=a.Id);
CampaignMember cm4 = new CampaignMember(CampaignId=c4.Id, ContactId=a.Id);
List<CampaignMember> campaignMemberList = new List<CampaignMember> { cm1, cm2, cm3, cm4 };
INSERT campaignMemberList;
//Create mock RTP Relationship records
Relationship__c r1 = new Relationship__c(Contact__c=a.Id, State_Date__c = '2013-06-18');
Relationship__c r2 = new Relationship__c(Contact__c=a.Id, State_Date__c = '2013-07-15');
Relationship__c r3 = new Relationship__c(Contact__c=a.Id, State_Date__c = '2013-08-21');
Relationship__c r4 = new Relationship__c(Contact__c=a.Id, State_Date__c = '2013-02-10');
List<Relationship__c> rtpRelationshipList = new List<Relationship__c> { r1, r2, r3, r4 };
INSERT rtpRelationshipList;
//Create a mock Documentation record
Documentation__c d1 = new Documentation__c(Contact__c=a.Id)
INSERT d1;
//Create Repair Application records
Repair_Application__c ra1 = new Repair_Application__c(Documentation_Reference__c = d1.Id);
Repair_Application__c ra2 = new Repair_Application__c(Documentation_Reference__c = d1.Id);
List<Repair_Application__c> repairApplicationList = new List<Repair_Application__c> { ra1, ra2 };
INSERT repairApplicationList;
//Create Donation/Opportunity records
Opportunity o1 = new Opportunity(Name= 'TestOpp1', CloseDate = '2013-06-07');
Opportunity o2 = new Opportunity(Name= 'TestOpp2', CloseDate = '2013-07-15');
Opportunity o3 = new Opportunity(Name= 'TestOpp3', CloseDate = '2013-08-07');
List<Opportunity> opportunityList = new List<Opportunity> { o1, o2, o3 };
INSERT opportunityList;
//Create ContactRole entries
OpportunityContactRole ocr1 = new OpportunityContactRole(ContactId = a.Id, OpportunityId = o1.Id, Role = 'Donor');
OpportunityContactRole ocr2 = new OpportunityContactRole(ContactId = a.Id, OpportunityId = o2.Id, Role = 'Donor');
OpportunityContactRole ocr3 = new OpportunityContactRole(ContactId = a.Id, OpportunityId = o3.Id, Role = 'Donor');
List<OpportunityContactRole> opportunityContactRoleList = new List<OpportunityContactRole> { ocr1, ocr2, ocr3 };
INSERT opportunityContactRoleList;
householdIds.add(h.id);
//Create some mock Opportunities for the household.
Opportunity o1 = new Opportunity(name='Donation1', StageName = 'Posted', Amount = 500, Household__c = h.id, CloseDate = date.today(), AccountId = a.Id );
Opportunity o2 = new Opportunity(name='Donation2', StageName = 'Posted', Amount = 750, Household__c = h.id, CloseDate = date.today(), AccountId = a.Id );
Opportunity o3 = new Opportunity(name='Donation3', StageName = 'Posted', Amount = 250, Household__c = h.id, CloseDate = date.today(), AccountId = a.Id );
GenerateTopListResults testGenResults = new GenerateTopListResults(new ApexPages.StandardController(a));
testGenResults.getCampaignMemberVolunteerList();
testGenResults.getRelationshipList();
testGenResults.getDocumentationList();
testGenResults.getDonationList();
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Thank you k_bentsen and Souvik! I really appreciate it. I apoligize it has taken me SO LONG to refocus on this. When I use the following code for the test class I get this error: "Error: Compile Error: unexpected token: 'INSERT' at line 50 column 0." Am I inserting d1 correctly at line 50? I can't figure out why it doesn't accept that INSERT. Seems simple to me...?
Thank you both for your help!
Oh gosh! Of course. I found a few other issues with the code and I will post the updated lines after some research. I am trying to ensure I set the Campaign.StartDate properly to a value. I don't think you can say StartDate = '2013-08-09' for example. Thanks again for your help!
Right, you'll have to do StartDate = Date.newInstance(2013, 8, 9)
Let me know if there's anything else I can help with.
k_bensten...you rock! Thank you for your help with this. I think I am all clear here! This is my final product!