You need to sign in to do that
Don't have an account?

How to write a test class for multi select lookup page
I am having a super hard time in writing a test class. The extension lets a user select multiple records from child (Contingency__c) and add to the related list of parent (Billing_Change_form__c) where the account is the same. It contains a wrapper class. I have no idea how to get any coverage.
This is the test class that I have come up with so far:
@isTest
public class bcfTest {
public static testMethod void adBill(){
Billing_Change_form__c testBill = new Billing_Change_Form__c();
testBill.Name = 'Test name';
insert testBill;
Billing_Change_form__c testBill2 = new Billing_Change_Form__c();
testBill2.Name = 'Test name2';
insert testBill2;
Contingency__c caseTest = new Contingency__c();
caseTest.Name = 'test Case';
caseTest.Contingency__c = 'IN BILLING';
caseTest.VENDOR_INVOICE__r.Account_del__r.id='1234';
insert caseTest;
Contingency__c caseTest2 = new Contingency__c();
caseTest2.Name = 'test Case2';
caseTest2.Contingency__c = 'IN BILLING';
caseTest2.VENDOR_INVOICE__r.Account_del__r.id='5678';
insert caseTest2;
Account accTest = new Account();
accTest.Name = 'Test Account';
insert accTest;
Test.startTest();
PageReference pageRef = Page.bcTest; // Add your VF page Name here
pageRef.getParameters().put('id', String.valueOf(testBill.Id));
Test.setCurrentPage(pageRef);
List<wrapperCase> wrapList = new List<wrapperCase>();
List<Contingency__c> selCase = new List<Contingency__c>();
for(Contingency__c b : [Select id, name,
contingency__c, VENDOR_INVOICE__r.Account_del__r.Id from Contingency__c
where (VENDOR_INVOICE__r.Account_del__r.Id = :accTest.Id) ]){
wrapList.add(New wrapperCase(b));
}
for(wrapperCase WATest: wrapList){
WATest.check = true;
}
System.assertEquals(testBill.Id, caseTest.Bill_Form__c);
System.assertEquals('Test name',testBill.Name);
System.assertEquals('IN BILLING', caseTest.CONTINGENCY__c);
test.stopTest();
}
public class wrapperCase{
public contingency__c con{get;set;}
public boolean check{get;set;}
public wrapperCase(contingency__c c){
con = c;
check = false;
}
}
}
My controller extension:
public with sharing class test2 {
//Declare varialbles
public string lookup{get;set;}
public list<conCase> caseList{get;set;}
public boolean allbool{get;set;}
public string inputValue{get;set;}
public boolean bool{get;set;}
public set<id> caseIds{get;set;}
Billing_Change_form__c[] accID = [Select account__c from Billing_Change_Form__c where
id = :System.currentPagereference().getParameters().get('id')];
ApexPages.StandardController controller;
//Constructor
public test2(ApexPages.StandardController con){
controller = con;
caseList = new list<conCase>();
bool = false;
caseIds = new Set<id>();
}
//Event on clicking the checkbox
public void inIt(){
List<Contingency__c> selectedCase = new list<Contingency__c>();
lookUp = '';
for(conCase conObj : caseList){
if(conObj.check != False){
system.debug('conObj.con'+ conObj.con);
selectedCase.add(conObj.con);
lookUp += conObj.con.name + ', ';
system.debug('lookup:'+ lookup);
caseIds.add(conObj.con.id);
bool = true;
}
If(conObj.check != true){
caseIds.remove(conObj.con.id); }
}
}
//Displaying the records
public List<conCase> getShow(){
caseList = new list <conCase> ();
for(Contingency__c coObj : [Select id, name,billing_type__c, report_description__c, discovery_amount__c,
billing_begins_date__c, contingency__c, VENDOR_INVOICE__r.Account_del__r.Id from Contingency__c
where (VENDOR_INVOICE__r.Account_del__r.Id = :accID[0].Account__c) AND (Bill_form__c = null)])
{
caseList.add(new conCase(coObj, Bool));
}
return caseList;
}
//Event on saving the selected records
public PageReference mySave(){
list<Contingency__c> updateSelectedCase = new list<Contingency__c>();
id billId = ApexPages.currentPage().getparameters().get('id');
System.debug('Bill ID is' + billID);
for(Contingency__c co:[select id, name, bill_form__c from Contingency__c where id = :caseIds ]){
co.bill_form__c = billId;
updateSelectedCase.add(co);
}
update updateSelectedCase;
return null;
}
//Event When the cancel button is hit
public void closePopup(){
bool = false;
}
public void add(){
bool = true;
}
//Wrapper class for the selected records
public class conCase{
public contingency__c con{get;set;}
public boolean check{get;set;}
public conCase(contingency__c c, boolean boo){
con = c;
check = boo;
}
}
}
My visualforce Page:
<apex:page showHeader="False" standardController="Billing_Change_Form__c" extensions="test2">
<script>
function closeWindow(){
window.opener.location.href="/{!$CurrentPage.parameters.id}";
window.top.close();
}
</script>
<apex:form >
<apex:PageBlock title="CaseFindings" id="counter">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!mySave}" onComplete="closeWindow();"/>
<apex:commandButton value="Cancel" onclick="window.top.close();" immediate="true" action="{!closePopup}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!show}" var="e" title="show">
<apex:column >
<apex:inputCheckbox value="{!e.check}" />
<apex:actionSupport event="onclick" action="{!inIt}" rerender="none"/>
</apex:column>
<apex:column value="{!e.con.Name}"/>
<apex:column value="{!e.con.DISCOVERY_AMOUNT__c}"/>
<apex:column value="{!e.con.BILLING_BEGINS_DATE__c}"/>
<apex:column value="{!e.con.REPORT_DESCRIPTION__c}"/>
<apex:column value="{!e.con.CONTINGENCY__c}"/>
</apex:pageBlockTable>
</apex:PageBlock>
</apex:form>
</apex:page>
Hoping to get standard 75% coverage. If anyone can help me with it, I would really appreciate it! Thanks!
This is the test class that I have come up with so far:
@isTest
public class bcfTest {
public static testMethod void adBill(){
Billing_Change_form__c testBill = new Billing_Change_Form__c();
testBill.Name = 'Test name';
insert testBill;
Billing_Change_form__c testBill2 = new Billing_Change_Form__c();
testBill2.Name = 'Test name2';
insert testBill2;
Contingency__c caseTest = new Contingency__c();
caseTest.Name = 'test Case';
caseTest.Contingency__c = 'IN BILLING';
caseTest.VENDOR_INVOICE__r.Account_del__r.id='1234';
insert caseTest;
Contingency__c caseTest2 = new Contingency__c();
caseTest2.Name = 'test Case2';
caseTest2.Contingency__c = 'IN BILLING';
caseTest2.VENDOR_INVOICE__r.Account_del__r.id='5678';
insert caseTest2;
Account accTest = new Account();
accTest.Name = 'Test Account';
insert accTest;
Test.startTest();
PageReference pageRef = Page.bcTest; // Add your VF page Name here
pageRef.getParameters().put('id', String.valueOf(testBill.Id));
Test.setCurrentPage(pageRef);
List<wrapperCase> wrapList = new List<wrapperCase>();
List<Contingency__c> selCase = new List<Contingency__c>();
for(Contingency__c b : [Select id, name,
contingency__c, VENDOR_INVOICE__r.Account_del__r.Id from Contingency__c
where (VENDOR_INVOICE__r.Account_del__r.Id = :accTest.Id) ]){
wrapList.add(New wrapperCase(b));
}
for(wrapperCase WATest: wrapList){
WATest.check = true;
}
System.assertEquals(testBill.Id, caseTest.Bill_Form__c);
System.assertEquals('Test name',testBill.Name);
System.assertEquals('IN BILLING', caseTest.CONTINGENCY__c);
test.stopTest();
}
public class wrapperCase{
public contingency__c con{get;set;}
public boolean check{get;set;}
public wrapperCase(contingency__c c){
con = c;
check = false;
}
}
}
My controller extension:
public with sharing class test2 {
//Declare varialbles
public string lookup{get;set;}
public list<conCase> caseList{get;set;}
public boolean allbool{get;set;}
public string inputValue{get;set;}
public boolean bool{get;set;}
public set<id> caseIds{get;set;}
Billing_Change_form__c[] accID = [Select account__c from Billing_Change_Form__c where
id = :System.currentPagereference().getParameters().get('id')];
ApexPages.StandardController controller;
//Constructor
public test2(ApexPages.StandardController con){
controller = con;
caseList = new list<conCase>();
bool = false;
caseIds = new Set<id>();
}
//Event on clicking the checkbox
public void inIt(){
List<Contingency__c> selectedCase = new list<Contingency__c>();
lookUp = '';
for(conCase conObj : caseList){
if(conObj.check != False){
system.debug('conObj.con'+ conObj.con);
selectedCase.add(conObj.con);
lookUp += conObj.con.name + ', ';
system.debug('lookup:'+ lookup);
caseIds.add(conObj.con.id);
bool = true;
}
If(conObj.check != true){
caseIds.remove(conObj.con.id); }
}
}
//Displaying the records
public List<conCase> getShow(){
caseList = new list <conCase> ();
for(Contingency__c coObj : [Select id, name,billing_type__c, report_description__c, discovery_amount__c,
billing_begins_date__c, contingency__c, VENDOR_INVOICE__r.Account_del__r.Id from Contingency__c
where (VENDOR_INVOICE__r.Account_del__r.Id = :accID[0].Account__c) AND (Bill_form__c = null)])
{
caseList.add(new conCase(coObj, Bool));
}
return caseList;
}
//Event on saving the selected records
public PageReference mySave(){
list<Contingency__c> updateSelectedCase = new list<Contingency__c>();
id billId = ApexPages.currentPage().getparameters().get('id');
System.debug('Bill ID is' + billID);
for(Contingency__c co:[select id, name, bill_form__c from Contingency__c where id = :caseIds ]){
co.bill_form__c = billId;
updateSelectedCase.add(co);
}
update updateSelectedCase;
return null;
}
//Event When the cancel button is hit
public void closePopup(){
bool = false;
}
public void add(){
bool = true;
}
//Wrapper class for the selected records
public class conCase{
public contingency__c con{get;set;}
public boolean check{get;set;}
public conCase(contingency__c c, boolean boo){
con = c;
check = boo;
}
}
}
My visualforce Page:
<apex:page showHeader="False" standardController="Billing_Change_Form__c" extensions="test2">
<script>
function closeWindow(){
window.opener.location.href="/{!$CurrentPage.parameters.id}";
window.top.close();
}
</script>
<apex:form >
<apex:PageBlock title="CaseFindings" id="counter">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!mySave}" onComplete="closeWindow();"/>
<apex:commandButton value="Cancel" onclick="window.top.close();" immediate="true" action="{!closePopup}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!show}" var="e" title="show">
<apex:column >
<apex:inputCheckbox value="{!e.check}" />
<apex:actionSupport event="onclick" action="{!inIt}" rerender="none"/>
</apex:column>
<apex:column value="{!e.con.Name}"/>
<apex:column value="{!e.con.DISCOVERY_AMOUNT__c}"/>
<apex:column value="{!e.con.BILLING_BEGINS_DATE__c}"/>
<apex:column value="{!e.con.REPORT_DESCRIPTION__c}"/>
<apex:column value="{!e.con.CONTINGENCY__c}"/>
</apex:pageBlockTable>
</apex:PageBlock>
</apex:form>
</apex:page>
Hoping to get standard 75% coverage. If anyone can help me with it, I would really appreciate it! Thanks!
All Answers
It had zero coverage.
After adding some required fields. I am getting 39% coverage. Here is the error that I am getting.
System.ListException: List index out of bounds: 0
Class.test2.getShow: line 48, column 1
Class.bcTest.adBill: line 56, column 1
Updated test class:
@isTest
public class bcTest {
public static testMethod void adBill(){
Billing_Change_form__c testBill = new Billing_Change_Form__c();
testBill.Name = 'Test name';
insert testBill;
Billing_Change_form__c testBill2 = new Billing_Change_Form__c();
testBill2.Name = 'Test name2';
insert testBill2;
Account accTest = new Account();
accTest.Name = 'Test Account';
insert accTest;
Account accTest2 = new Account();
accTest2.Name = 'Test Account2';
insert accTest2;
Account_number__c venTest = new Account_Number__c();
venTest.Name = 'Test Ven';
venTest.Account_del__c = accTest.id;
insert venTest;
Account_number__c venTest2 = new Account_Number__c();
venTest2.Name = 'Test Ven2';
venTest2.Account_del__c = accTest2.id;
insert venTest2;
Contingency__c caseTest = new Contingency__c();
caseTest.Name = 'test Case';
caseTest.Contingency__c = 'IN BILLING';
caseTest.vendor_invoice__c = venTest.id;
insert caseTest;
Contingency__c caseTest2 = new Contingency__c();
caseTest2.Name = 'test Case2';
caseTest2.Contingency__c = 'IN BILLING';
caseTest2.vendor_invoice__c = venTest2.id;
insert caseTest2;
Test.startTest();
ApexPages.StandardController sc = new ApexPages.StandardController(testBill);
test2 testAccPlan = new test2(sc);
PageReference pageRef = Page.bcTest; // Add your VF page Name here
pageRef.getParameters().put('id', String.valueOf(testBill.Id));
Test.setCurrentPage(pageRef);
testAccPlan.inIt();
testAccPlan.getShow();
testAccPlan.mySave();
testAccPlan.closePopup();
testAccPlan.add();
test.stopTest();
}
}
where (VENDOR_INVOICE__r.Account_del__r.Id = :accID[0].Account__c) AND (Bill_form__c = null)]
that check variable is part of the wrapper class. How do I set to false or true to validate it with system.asserts?