I have a custom VF controller. I have written a unit test that covers much of it, however, I am not able to cover the page re-direct upon save in my controller (lines 33-37). Does anyone know how I can accomplish this?


//Controller for Deal Summary
public class VF_DealSummaryController{

public List<Deal_Summary__c> DS {get; set;}

    private final Opportunity opp;
    public VF_DealSummaryController(ApexPages.StandardController myController){
        DS = new List<Deal_Summary__c>();

    public Deal_Summary__c DS2 = new Deal_Summary__c();
        public void DealSummary(){

            DS2.Opportunity__c = opp.Id;
            DS2.Legal_Name__c = opp.Account.Id;

    public PageReference save() {

        Boolean error=false;

        IF(DS2.Agreement_Effective_Date__c == null){
            DS2.Agreement_Effective_Date__c.addError('You must enter an estimated Effective Date.');
            error = true;
        IF(DS2.Net_Payment_Term_Days__c == null){
            DS2.Net_Payment_Term_Days__c.addError('You must enter the Net Payment terms.');
            error = true;
        IF (error) {return null;}
    insert DS;
        PageReference RetPage = new PageReference('/apex/DealSummaryViewTest?id=' + DS[0].id + '#RTF');
        return RetPage; 

Test Class:

private class TestDealSummController
    static testMethod void testMyController1()
        Account acct1 = TestCreateRecords.createAcct(0);
        insert acct1;

        Opportunity opp1 = TestCreateRecords.createOppNew(acct1.Id);
        insert opp1;

        Deal_Summary__c DS1 = new Deal_Summary__c();
            DS1.Opportunity__c = opp1.Id;
            DS1.Legal_Name__c = opp1.AccountId;
            DS1.Agreement_Effective_Date__c = date.newinstance(2025,1,31);
            DS1.Net_Payment_Term_Days__c = 30;

        ApexPages.StandardController DealSumm1 = new ApexPages.standardController(opp1);
        VF_DealSummaryController DealSummCont1 = new VF_DealSummaryController(DealSumm1);

            DealSummCont1.DS2.Net_Payment_Term_Days__c = 30;

            DealSummCont1.DS2.Net_Payment_Term_Days__c = 50;
  • What are lines 33-37?
    – Eric
    Oct 8 '15 at 19:28
  • Everything from "Insert DS" down in my controller.
    – J. Neilan
    Oct 8 '15 at 20:30

Your test is missing:

DealSummCont1.DS2.Agreement_Effective_Date__c = Date.today();

so the controller line:

IF (error) {return null;}

means the code after that never runs in your test case.

  • I don't understand. I do have a value set in my test for Agreement_Effective_Date__c, why would it not get past the controller line you specified?
    – J. Neilan
    Oct 9 '15 at 15:50
  • It is th DS2 reference you need to set and at present you are setting the other field twice on DS2.
    – Keith C
    Oct 9 '15 at 16:58
  • Thanks. So are you saying that I need to set values on the DS2 reference that will bypass the validations in my controller?
    – J. Neilan
    Oct 9 '15 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.