function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
John Neilan 2John Neilan 2 

Test for Visualforce Controller

Hello,

I have a custom VF controller that is very simple, but has some valudations built in upon save.  I am trying to run a test to cover the controller, but the test I am running is not covering lines that I think it should.  Lines 12-16, 32 & 33, and 36 & 37 are not covered, but I specifically have the criteria in my test to fire those rules.  Does anyone know what I am missing to cover those lines?  Thanks!

VF Controller:
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>();
        opp=(Opportunity)myController.getrecord();
    }

    public Deal_Summary__c DS2 = new Deal_Summary__c();
        public void DealSummary(){
       
            DS2.Opportunity__c = opp.Id;
            DS2.Legal_Name__c = opp.Account.Id;
            DS.add(DS2);
        }

    public PageReference save() {
    
        Boolean error=false;
        
        IF(DS2.Effective_Date__c == null){
            DS2.Effective_Date__c.addError('You must enter an 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(DS2.Net_Payment_Term_Days__c < 45){
            DS2.Net_Payment_Term_Days__c.addError('Net Payment Terms cannot be less than 45 days.');
            error = true;
        }
        IF(DS2.Auto_Renewal__c == TRUE && DS2.Notice_To_Terminate_Days__c == null){
            DS2.Notice_To_Terminate_Days__c.addError('Please input the # of days notice.');
            error = true;
        }
        IF (error) {return null;}
    insert DS;
        {
        PageReference RetPage = new PageReference('/apex/DealSummaryViewTest?id=' + DS[0].id + '#RTF');
        RetPage.setRedirect(true);
        return RetPage; 
       }
    }
}

Controller Test:
public class TestDSController {

public 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.Effective_Date__c = date.newinstance(2025,1,31);
      DS1.Net_Payment_Term_Days__c = 30;
      DS1.Auto_Renewal__c = TRUE;
      DS1.Notice_To_Terminate_Days__c = null;
 
    ApexPages.StandardController DealSumm1 = new ApexPages.standardController(opp1);
    VF_DealSummaryController DealSummCont1 = new VF_DealSummaryController(DealSumm1);
    DealSummCont1.DS.add(DS1);
    DealSummCont1.save();
}
}

 
Best Answer chosen by John Neilan 2
Neetu_BansalNeetu_Bansal
Hi John,

Use the updated code:
public class TestDSController {

public 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.Effective_Date__c = date.newinstance(2025,1,31);
      DS1.Net_Payment_Term_Days__c = 30;
      DS1.Auto_Renewal__c = TRUE;
      DS1.Notice_To_Terminate_Days__c = null;
 
    ApexPages.StandardController DealSumm1 = new ApexPages.standardController(opp1);
    VF_DealSummaryController DealSummCont1 = new VF_DealSummaryController(DealSumm1);
    DealSummCont1.DS.add(DS1);
    DealSummCont1.DealSummary();
    DealSummCont1.save();
}
}
Let me know, if you need any other help.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi John,

Use the updated code:
public class TestDSController {

public 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.Effective_Date__c = date.newinstance(2025,1,31);
      DS1.Net_Payment_Term_Days__c = 30;
      DS1.Auto_Renewal__c = TRUE;
      DS1.Notice_To_Terminate_Days__c = null;
 
    ApexPages.StandardController DealSumm1 = new ApexPages.standardController(opp1);
    VF_DealSummaryController DealSummCont1 = new VF_DealSummaryController(DealSumm1);
    DealSummCont1.DS.add(DS1);
    DealSummCont1.DealSummary();
    DealSummCont1.save();
}
}
Let me know, if you need any other help.

Thanks,
Neetu
This was selected as the best answer
John Neilan 2John Neilan 2
Thanks Neetu!  That change helped cover lines 12-16, but I'm still not getting coverage on all the IF statements in my controller, even though I have them firing in the test.  Any idea why?
Neetu_BansalNeetu_Bansal
Hi John,

Everthing seems to be perfect, is it possible to share the salesforce org login credentials, so I can have a look and help you out.
You can contact me either on my gmail id: neetu.bansal.5@gmail.com or Skype id: neetu.bansal.5

Thanks,
Neetu
John Neilan 2John Neilan 2
Hi Neetu,

I will email you.  Thanks!
Shyama B SShyama B S
Hi John,
You are not assigning values to the DS2 object in your test class. I think that is the reason why it is not covering those lines.