You need to sign in to do that
Don't have an account?

Finishing up Test Class for controller extension
I have a controller extension for a VF page that I got 60% coverage on. The only part that I am having issues covering is a query of campaigns. Below is my Class:
1 public class TradeShowLeads1 { 2 3 //Create list for leads 4 private final Lead ld; 5 List<Lead> lds = new List<Lead>(); 6 List<CampaignMember> CamMems = new List<CampaignMember>(); 7 Campaign c1 = [Select ID, Primary_Market__c, Name from Campaign where Campaign_Form__c = 'Form 1' LIMIT 1]; 8 9 public TradeShowLeads1 (ApexPages.StandardController controller) { 10 ld=(Lead)controller.getrecord(); 11 ld.LeadSource = 'Trade Show'; 12 ld.Industry = c1.Primary_Market__c; 13 ld.Lead_Type__c = 'Inbound'; 14 } 15 16 //create list for Campaign 17 Campaign campaign; 18 List<Campaign> campaigns; 19 20 public List<Campaign> getCampaigns() { 21 if(campaigns == null) { 22 campaigns = new List<Campaign>(); 23 campaign=null; 24 List<Campaign> campaigns2 = new List<Campaign>(); 25 campaigns2 = [Select Name from Campaign where Campaign_Form__c = 'Form 1' LIMIT 1]; 26 for(Campaign campaign:campaigns2) { 27 campaigns.add(campaign); 28 } 29 } 30 return campaigns; 31 } 32 33 public PageReference TradeShowLeads1() { 34 if(ld.get('Id') == null){ 35 lds.add(ld); 36 CampaignMember mem = new CampaignMember (campaignid=c1.id, leadid=ld.id); 37 CamMems.add(mem); 38 } 39 40 insert lds; 41 insert CamMems; 42 PageReference RedirectLead = new PageReference('/apex/tradeshow1'); 43 redirectLead.setRedirect(true); 44 return RedirectLead; 45 46 47 } 48 49 50 51 }
Below is my test class:
public class TestTradeShowExtension1 { static testMethod void testTradeShow1() { //create the Lead Lead testL = new Lead(LastName = 'test', Company = 'test', Industry = 'test', LeadSource = 'Online Search', Lead_Type__c = 'inbound', Contact_Method__c = 'Phone'); insert testL; //create Campaign Campaign testC = new Campaign(Name = 'test', StartDate = Date.today(), Primary_Market__c = 'Fitness'); insert testC; //create standard controller for lead ApexPages.StandardController stdCon = new ApexPages.StandardController(testL); //create controller extension with stdCon TradeShowLeads1 ext = new TradeShowLeads1(stdCon); //assert campaign member was created //assert the pagereference worked ext.TradeShowLeads1(); } }
When I run the test I am missing coverage on lines 20 - 30 and lines 35-37. Any help would be greatly appreciated.
try this...
public class TestTradeShowExtension1 {
static testMethod void testTradeShow1() {
//create the Lead
Lead testL = new Lead(LastName = 'test',
Company = 'test',
Industry = 'test',
LeadSource = 'Online Search',
Lead_Type__c = 'inbound',
Contact_Method__c = 'Phone');
insert testL;
//create Campaign
Campaign testC = new Campaign(Name = 'test',
StartDate = Date.today(),
Primary_Market__c = 'Fitness');
insert testC;
//create standard controller for lead
ApexPages.StandardController stdCon = new ApexPages.StandardController(testL);
//create controller extension with stdCon
TradeShowLeads1 ext = new TradeShowLeads1(stdCon);
//assert campaign member was created
//assert the pagereference worked
ext.getCampaigns();
ext.TradeShowLeads1();
in order to cover 35-37 u need the id value as null means u need to pass instance of the object with out inserting the record
}
}
All Answers
try this...
public class TestTradeShowExtension1 {
static testMethod void testTradeShow1() {
//create the Lead
Lead testL = new Lead(LastName = 'test',
Company = 'test',
Industry = 'test',
LeadSource = 'Online Search',
Lead_Type__c = 'inbound',
Contact_Method__c = 'Phone');
insert testL;
//create Campaign
Campaign testC = new Campaign(Name = 'test',
StartDate = Date.today(),
Primary_Market__c = 'Fitness');
insert testC;
//create standard controller for lead
ApexPages.StandardController stdCon = new ApexPages.StandardController(testL);
//create controller extension with stdCon
TradeShowLeads1 ext = new TradeShowLeads1(stdCon);
//assert campaign member was created
//assert the pagereference worked
ext.getCampaigns();
ext.TradeShowLeads1();
in order to cover 35-37 u need the id value as null means u need to pass instance of the object with out inserting the record
}
}
So simple, thank you!!