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

Unit Test of Visualforce page - PageReference add() method

Hi - I have the following visualforce page with controller that allows for multiple row entry on a custom object. I'm stuck on how to get coverage for the Pagereference add() method.  How do I get test data added to the visualforce page so that I can test it? the data is a list passed from the controller..


Thanks in advance!


Visualforce Page based on custom object Term__C which is the child of the Contract object in a master-detail relationship.

<apex:page controller="multiAddVC" sidebar="false" id="VCPage" showheader="true" tabstyle="contract">
<script language="JavaScript" type="text/javascript">
function CloseAndRefresh(){
window.parent.location.href="/{!$}"; self.close(); }
<apex:pageMessages />
<apex:form >
<apex:pageBlock title="Virtual Contract - Financial Terms">
<apex:pageBlockButtons >
<apex:pagemessages rendered="true" id="msg2" />
<apex:commandButton value="Add New" action="{!add}" rerender="rows" status="outStatus"/>
<apex:commandButton value="Clear Unsaved" action="{!reset}" rerender="rows" status="outStatus" immediate="true" />
<apex:commandButton action="{!save}" value="Save and Close" oncomplete="javascript&colon;CloseAndRefresh()" rerender="rows,msg2" status="outStatus"/>
<apex:pageBlockTable width="100%" align="center" value="{!cexs}" var="c" id="rows" >
<apex:column headervalue="REF">
    <apex:outputText value="{!c.Name}"/>
<apex:column headerValue="Category">
    <apex:inputField value="{!c.MainCategory__c}" required="true"/>
<apex:column headervalue="Sub">
    <apex:inputField value="{!c.Category__c}" required="true"/>
<apex:column headervalue="Reference">
    <apex:inputField value="{!c.Contract_Reference__c}" required="true"/>
<apex:column headervalue="Financial Term">
    <apex:inputField value="{!c.Financial_Term__c}" required="true"/>
<apex:column headervalue="Other Info">
    <apex:inputField value="{!c.Short_Description__c}" style="width: 300px;height:50px"/>
<apex:column headervalue="Frequency">
    <apex:inputField value="{!c.Frequency__c}" required="true"/>
<apex:column headervalue="Approved?">
    <apex:outputText value="{!c.Approved__c}"/>
<apex:column headervalue="Active?">
    <apex:outputText value="{!c.Active_Term__c}"/>




public class multiAddVC {
List <Term__c> VCList;
public Id cID = ApexPages.currentPage().getParameters().get('Id'); //grab the Contract ID
public Id getID {get; set;}

public PageReference reset(){ 
VCList = [select name, Contract__c, Category__c,maincategory__c, contract_reference__c,short_description__c,frequency__c,
financial_term__c,active_term__c,approved__c from Term__c where Contract__c =: cID order by createddate ];
return null; 
        public List <term__c> getCExs(){
        if(VCList == null) reset();
    Return VCList;
public void setAccounts(List <Term__c> cexs) {
   VCList = cexs;}
public PageReference save() {//upsert records on save
upsert VCList;
ApexPages.Message myMsg = new ApexPages.message(ApexPages.Severity.Info, 'Records Saved Successfully'); //show confirmation message on save
return null;}
public PageReference add() {
VCList.add(New Term__c(Contract__c = cID)); //add records to Virtual contract and associate with current Contract
return null; }


Unit Test 

public class MultiAddVCTests {
static testmethod void MultiAddVCControllerTests(){

// create a new account

account testaccount = new Account(Name='TEST COMPANY',Region__c='EMEA');
insert testaccount;

account testaccount2 = new Account(Name='EMEA REGION',Industry='System use only',Market__c='HQ', Type='Other', Group__c='Global Network Partnerships',Region__c='EMEA');
insert testaccount2;

// create an LFO contact
Contact lfo = new Contact(LastName = 'Hello LFO',business_unit__c='Regional LFO team',,email='');

      insert lfo;

// create a BDL contact
Contact bdl = new Contact(LastName = 'Hello BDL',business_unit__c='GNS Business',,email='');

      insert bdl;

// Add a new contract

contract c = new Contract();{
//c.status='Not Started';;;;;;;;
c.Project_plan_approved__c=true;'Test Contract';

insert c;


// set-up a Virtual contract record

term__c VC = new Term__c(,short_description__c='test short',contract_reference__c='test contract ref',approved__c=false,
    contract_language__c='Test contract language',frequency__c='Other',active_term__c='Yes');

insert vc;

// Set up visualforce parameters

PageReference pageRef = Page.MultiAddVC;


MultiAddVC controller = new MultiAddVC();

// create an instance of the controller
// Get a list of Terms already in the system and make sure they are displayed on Page
List<Term__c> Terms = ([select name, Contract__c, Category__c,maincategory__c, 
approved__c from Term__c where Contract__c]); 

List<Term__c> VFTerms = controller.getCEXs();