I'm having trouble getting adequate test coverage for a custom VF controller I created. My test and controller are below. When I run the test, I am getting no coverage for lines 13, 16-17, and 26-46. Can anyone point me to what I need to do to get these lines covered? The VF page works properly when tested. Thanks.
Test Code:
@Istest(SeeAllData=true) public class TestVFControllers {
public static testMethod void Test_CQ_Overall() {
User user1 = TestCreateRecords.createAMUser();
insert user1;
Account acct1 = TestCreateRecords.createAcct(0);
insert acct1;
Client_Questionnaire__c cq1 = TestCreateRecords.createCQ(acct1.Id);
ApexPages.StandardController sc1 = new ApexPages.standardController(acct1);
VF_CQController cqCont1 = new VF_CQController(sc1);
cqCont1.cq.add(cq1);
Test.StartTest();
cqCont1.clientQuest();
Test.StopTest();
}
public static testMethod void Test_CQ_InitMtgValue() {
User user2 = TestCreateRecords.createAMUser();
insert user2;
Account acct2 = TestCreateRecords.createAcct(0);
insert acct2;
//Test for Initial Meeting Date
Client_Questionnaire__c cq2 = TestCreateRecords.createCQ(acct2.Id);
cq2.Initial_Meeting_Date__c = Date.Today().addDays(0);
cq2.Primary_Goal__c = null;
ApexPages.StandardController sc2 = new ApexPages.standardController(acct2);
VF_CQController cqCont2 = new VF_CQController(sc2);
cqCont2.cq.add(cq2);
Test.StartTest();
cqCont2.save();
Test.StopTest();
}
public static testMethod void Test_CQ_PrimGoalValue() {
User user3 = TestCreateRecords.createAMUser();
insert user3;
Account acct3 = TestCreateRecords.createAcct(0);
insert acct3;
//Test for Primary Social Goal
Client_Questionnaire__c cq3 = TestCreateRecords.createCQ(acct3.Id);
cq3.Primary_Goal__c = 'Build awareness';
ApexPages.StandardController sc3 = new ApexPages.standardController(acct3);
VF_CQController cqCont3 = new VF_CQController(sc3);
cqCont3.cq.add(cq3);
Test.StartTest();
cqCont3.save();
Test.StopTest();
}
public static testMethod void testCQ4() {
User user4 = TestCreateRecords.createAMUser();
insert user4;
Account acct4 = TestCreateRecords.createAcct(0);
insert acct4;
//Test for Training
Client_Questionnaire__c cq4 = TestCreateRecords.createCQ(acct4.Id);
cq4.Training__c = 'Scheduled';
ApexPages.StandardController sc4 = new ApexPages.standardController(acct4);
VF_CQController cqCont4 = new VF_CQController(sc4);
cqCont4.cq.add(cq4);
Test.StartTest();
cqCont4.save();
Test.StopTest();
}
public static testMethod void testCQ5() {
User user5 = TestCreateRecords.createAMUser();
insert user5;
Account acct5 = TestCreateRecords.createAcct(0);
insert acct5;
//Test for Training Availability
Client_Questionnaire__c cq5 = TestCreateRecords.createCQ(acct5.Id);
cq5.Training_Availability__c = 'Anytime';
ApexPages.StandardController sc5 = new ApexPages.standardController(acct5);
VF_CQController cqCont5 = new VF_CQController(sc5);
cqCont5.cq.add(cq5);
Test.StartTest();
cqCont5.save();
Test.StopTest();
}
public static testMethod void testCQ6() {
User user6 = TestCreateRecords.createAMUser();
insert user6;
Account acct6 = TestCreateRecords.createAcct(0);
insert acct6;
//Test for Training Availability
Client_Questionnaire__c cq6 = TestCreateRecords.createCQ(acct6.Id);
cq6.Recurring_Meeting__c = 'Monthly';
ApexPages.StandardController sc6 = new ApexPages.standardController(acct6);
VF_CQController cqCont6 = new VF_CQController(sc6);
cqCont6.cq.add(cq6);
Test.StartTest();
cqCont6.save();
Test.StopTest();
}
}
Custom Controller:
public class VF_CQController{
public List<Client_Questionnaire__c> cq {get; set;}
private final Account acct;
public VF_CQController(ApexPages.StandardController myController){
cq = new List<Client_Questionnaire__c>();
acct=(Account)myController.getrecord();
system.debug('*##***** acct: ' + acct);
}
public Client_Questionnaire__c cq2 = new Client_Questionnaire__c();
public void clientQuest(){
system.debug('*##*****:'+cq2);
cq2.Account_Name__c = acct.id;
cq.add(cq2);
system.debug('*##***** Account Name: '+cq2.Account_Name__c);
}
public PageReference save() {
IF(cq2.Initial_Meeting_Date__c == null){
cq2.Initial_Meeting_Date__c.addError('Please enter the date of the initial meeting you have with this client');
return null;
}
IF(cq2.Primary_Goal__c == null){
cq2.Primary_Goal__c.addError('Please enter the Primar Goal for this client');
return null;
}
IF(cq2.Training__c == null){
cq2.Training__c.addError('Please enter the initial training you have set up for this client');
return null;
}
IF(cq2.Training_Availability__c == null){
cq2.Training_Availability__c.addError('Please enter the client’s availability for training');
return null;
}
IF(cq2.Recurring_Meeting__c == null){
cq2.Recurring_Meeting__c.addError('Please enter the recurring meeting schedule set up for this client');
return null;
}
insert cq;
{
PageReference RetPage = new PageReference('/apex/ClientQuestInitVerify?id=' + cq[0].id);
RetPage.setRedirect(true);
return RetPage;
}
}
}
SeeAllData=true
should be avoided as it can lead to issues in deployment; use test data factories and/or mock environment methods (e.g. for custom settings) prior to the Test.startTest().