You need to sign in to do that
Don't have an account?
John Neilan 18
Test Class to Cover VF Dynamic Removal of Rows
Hi,
I have a VF page that allows a user to dynamically add and remove rows before saving each row as a new custom object record. I am trying to put together a test class to cover the removal part of my controller, but I can't seem to get any values through to the test as I am getting an error:
What am I missing?
Controller
Test Class
I have a VF page that allows a user to dynamically add and remove rows before saving each row as a new custom object record. I am trying to put together a test class to cover the removal part of my controller, but I can't seem to get any values through to the test as I am getting an error:
System.NullPointerException: Argument cannot be null. Class.NewRecController.RemoveType: line 24, column 1 Class.Test_NewRecController.testMethod1: line 27, column 1
What am I missing?
Controller
public class NewRecController { Rec_Type__c rec = new Rec_Type__c(); public list<Rec_Type__c> listRecType{ get; set; } Id acctId; Id contId; Public Integer rec {get; set;} // Constructor public NewRecController() { acctId = ApexPages.currentPage().getParameters().get('acctId'); contId = ApexPages.currentPage().getParameters().get('contId'); rec.Account__c = acctId; rec.Contact__c = contId; listRecType = new list<Rec_Type__c>(); listRecType.add(rec); } public PageReference RemoveType() { rec = Integer.valueOf(ApexPages.currentPage().getParameters().get('rec')); listRecType.remove(rec); return null; } public PageReference saveType() { try { insert listRecType; } catch(DmlException e) { ApexPages.addMessages(e); return null; } PageReference contRecord = new PageReference('/'+contId); contRecord.setRedirect(true); return contRecord; } }
Test Class
@isTest private class Test_NewRecController { static testMethod void testMethod1() { Account testAccount = test_CreateRecords.createAcct(0); insert testAccount; Contact testContact = test_CreateRecords.createCont(testAccount.Id); insert testContact; Test.StartTest(); PageReference pageRef = Page.NewRecType; pageRef.getParameters().put('contId','testContact.Id'); pageRef.getParameters().put('acctId','testAccount.Id'); pageRef.getParameters().put('Rec_Type__c','Type 2'); NewRecController testNewRec = new NewRecController(); testNewRec.rec = 1; testNewRec.RemoveType(); testNewRec.saveType(); Test.StopTest(); } }
I recommend adding a try and catch block as described here and doing null checks wherever necessary