You need to sign in to do that
Don't have an account?
Edwards71
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="/{!$CurrentPage.parameters.id}"; self.close(); } </script> <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:CloseAndRefresh()" rerender="rows,msg2" status="outStatus"/> </apex:pageBlockButtons> <apex:pageBlockTable width="100%" align="center" value="{!cexs}" var="c" id="rows" > <apex:column headervalue="REF"> <apex:outputText value="{!c.Name}"/> </apex:column> <apex:column headerValue="Category"> <apex:inputField value="{!c.MainCategory__c}" required="true"/> </apex:column> <apex:column headervalue="Sub"> <apex:inputField value="{!c.Category__c}" required="true"/> </apex:column> <apex:column headervalue="Reference"> <apex:inputField value="{!c.Contract_Reference__c}" required="true"/> </apex:column> <apex:column headervalue="Financial Term"> <apex:inputField value="{!c.Financial_Term__c}" required="true"/> </apex:column> <apex:column headervalue="Other Info"> <apex:inputField value="{!c.Short_Description__c}" style="width: 300px;height:50px"/> </apex:column> <apex:column headervalue="Frequency"> <apex:inputField value="{!c.Frequency__c}" required="true"/> </apex:column> <apex:column headervalue="Approved?"> <apex:outputText value="{!c.Approved__c}"/> </apex:column> <apex:column headervalue="Active?"> <apex:outputText value="{!c.Active_Term__c}"/> </apex:column> </apex:pageBlockTable> </apex:pageblock> </apex:form> </apex:page>
Controller 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 ApexPages.addMessage(myMsg); 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 @isTest 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',accountid=testaccount2.id,email='test2@test.com'); insert lfo; // create a BDL contact Contact bdl = new Contact(LastName = 'Hello BDL',business_unit__c='GNS Business',accountid=testaccount2.id,email='test2@test.com'); insert bdl; // Add a new contract contract c = new Contract();{ //c.status='Not Started'; c.bdl_contact__c=bdl.id; c.lfo_contact__c=lfo.id; c.accountid=testaccount.id; c.companysigneddate=system.today(); c.customersigneddate=system.today(); c.date_rec_d_in_gdi_inbox__c=system.today(); c.startdate=system.today(); c.Project_plan_approved__c=true; c.name='Test Contract'; c.agreement_type__c='Other'; c.mfr_required__c=false; c.BETA_test_required__c=false; insert c; } system.assert(true); // set-up a Virtual contract record term__c VC = new Term__c(contract__c=c.id,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; PageRef.getParameters().put('id',c.id); Test.setCurrentPage(pageRef); 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, contract_reference__c,short_description__c,frequency__c,financial_term__c,active_term__c, approved__c from Term__c where Contract__c =:c.id]); List<Term__c> VFTerms = controller.getCEXs(); system.assertEquals(Terms,VFTerms); controller.save(); controller.add(); } }