0

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&rsquo;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; 
        }
    }
}
2
  • 1
    For the lines 13 to 17 you don't have a test method that calls the method clientQuest. And for the others you have to create separate test methods because once it goes in the first IF it returns. Let me know if you need a code example. Commented Feb 5, 2015 at 21:00
  • 2
    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().
    – cropredy
    Commented Feb 6, 2015 at 0:34

1 Answer 1

1

First of all, try and name your unit test with a meaningful name. Follow the naming convention below. Also you might need to look at this documentation about testing in Salesforce, study it as testing it's an important part.

 public class TestVFControllers {

    static testMethod void UnitOfWork_StateUnderTest_ExpectedBehavior() {

         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 cqCont = new VF_CQController(sc1);
         cqCont.cq.add(cq1);
         Test.StartTest();
            cqCont.clentQuest();
         Test.StopTest();
    }
}

Like I said for the other you need to create a Client_Questionnaire__c record that will have a value for Initial_Meeting_Date__c so it doesn't go in the first if.

static testMethod void UnitOfWork_StateUnderTest_ExpectedBehavior() {
    
       //creating account
       Client_Questionnaire__c cq1 = TestCreateRecords.createCQ(acct1.Id);

       //This is where you set the so it jumps the first If
       cq1.Initial_Meeting_Date__c = 'YourValueHere';

       ApexPages.StandardController sc1 = new ApexPages.standardController(acct1);
       VF_CQController cqCont = new VF_CQController(sc1);
       cqCont.cq.add(cq1);
       Test.StartTest();
           cqCont.save();
       Test.StopTest();
}

UPDATE

Imagine how the program will run:

  1. You need to create an account
  2. Then instantiate the constructor which will automatically create a client questionnaire
  3. Set the various value so you pass the If
  4. Assert on the various output

With that in mind let's build our test

static testMethod void    VF_CQController_ClientQuestionnaireWithInitialMeetingDateAndPrimaryGoalSetToNull_ReturnError(){
  //method to create user
  //method to create account

  //instantiate controller
  ApexPages.StandardController sc = new ApexPages.standardController(acc);
  VF_CQController cqController = new VF_CQController(sc);

  //Artificially set the InitialMeetingDate to today so it passes the first if
  cqController.cq2.Initial_Meeting_Date__c = DateTime.Now();

  //Start the test by calling the method
  Test.StartTest();
     try{
       cqController.save();
     }
     catch(Exception ex){
       //Assert the output result, the parameters for this method are Expected, Actual
       System.AssertEquals('Your expected error message here', ex.getMessage()); 
  }
  Test.StopTest();    
}

You also don't need the SeeAllData=true as you create your own test data, delete the attribute.

I think this should help you and you could finish the rest of the tests.

And so on, you have to write the rest of test methods.

Hope it helps.

7
  • Thanks @Mihai. It did help. However, I think I may have misunderstood your answer for covering lines 26-46. I created separate test methods for each iteration that could be affected by the IF statements in my Class, but they are still not covered. What am I doing wrong? Thanks!
    – John N.
    Commented Feb 6, 2015 at 17:03
  • Please edit your answer with the updated test class Commented Feb 6, 2015 at 18:04
  • I tried, but it said I had too many characters. I'm still getting used to this site :-)
    – John N.
    Commented Feb 6, 2015 at 18:15
  • Edit your answer, do not post in the comment area Commented Feb 6, 2015 at 19:21
  • 1
    Thank @Mihai, that helped tremendously! I added the other test methods and now my controller is covered! Thanks for all your patience and assistance!
    – John N.
    Commented Feb 9, 2015 at 16:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .