You need to sign in to do that
Don't have an account?
Robert Bange*
How to create a test for a controler extention
I have created the following following controler extention and even after searching internet and forums, I do not succees in writing a test for it...
Can anyone help me?
public class passengerExtention
{
private final Individuele_Boeking__c boeking;
public string selectedArea {get; set;}
public passengerExtention(ApexPages.StandardController controller)
{
this.boeking = (Individuele_Boeking__c) controller.getRecord();
}
//Picklist Area
public list<selectoption> getAreaOptions()
{
//Verzamel waarden voor picklist
SelectOption firstOption = new SelectOption('', ' - Choose Area - ');
list<selectoption> Areas= new list<selectoption>();
Set<String> distinctAreas = new Set<String>();
list<Ships_location__c> selectedArea= [SELECT Area__c
FROM Ships_location__c
ORDER BY Area__c ];
for(Ships_location__c sl:selectedArea)
{
distinctAreas.add(sl.Area__c);
}
//Vul picklist
Areas.add(firstOption);
for(String s : distinctAreas)
{
Areas.add(new selectoption(s,s));
}
return Areas;
}
//Picklist Berth
public List<SelectOption> getBerthList()
{
List<SelectOption> passBerth = new List<SelectOption>();
//Verzamel waarden voor picklist
List<Ships_location__c> pBs = [SELECT id, Name
FROM Ships_location__c
WHERE Id NOT IN(select Berth__c from Opvarende__c o where o.Reis_Charter__c =: boeking.Reis__c)
AND Area__c =: selectedArea
ORDER BY name];
SelectOption firstOption = new SelectOption('', '- Choose Free Berth -');
//Vul picklist
passBerth.add(firstOption);
for (Ships_location__c pB: pBs)
{
passBerth.add(new SelectOption(pB.id, pB.Name ));
}
return passBerth;
}
}
Can anyone help me?
public class passengerExtention
{
private final Individuele_Boeking__c boeking;
public string selectedArea {get; set;}
public passengerExtention(ApexPages.StandardController controller)
{
this.boeking = (Individuele_Boeking__c) controller.getRecord();
}
//Picklist Area
public list<selectoption> getAreaOptions()
{
//Verzamel waarden voor picklist
SelectOption firstOption = new SelectOption('', ' - Choose Area - ');
list<selectoption> Areas= new list<selectoption>();
Set<String> distinctAreas = new Set<String>();
list<Ships_location__c> selectedArea= [SELECT Area__c
FROM Ships_location__c
ORDER BY Area__c ];
for(Ships_location__c sl:selectedArea)
{
distinctAreas.add(sl.Area__c);
}
//Vul picklist
Areas.add(firstOption);
for(String s : distinctAreas)
{
Areas.add(new selectoption(s,s));
}
return Areas;
}
//Picklist Berth
public List<SelectOption> getBerthList()
{
List<SelectOption> passBerth = new List<SelectOption>();
//Verzamel waarden voor picklist
List<Ships_location__c> pBs = [SELECT id, Name
FROM Ships_location__c
WHERE Id NOT IN(select Berth__c from Opvarende__c o where o.Reis_Charter__c =: boeking.Reis__c)
AND Area__c =: selectedArea
ORDER BY name];
SelectOption firstOption = new SelectOption('', '- Choose Free Berth -');
//Vul picklist
passBerth.add(firstOption);
for (Ships_location__c pB: pBs)
{
passBerth.add(new SelectOption(pB.id, pB.Name ));
}
return passBerth;
}
}
- create a record for Individuele_Boeking__c
for eg. Individuele_Boeking__c a = new Individuele_Boeking__c(name='aaa');
insert a;
- then mimic the page such as you have and Id of the object in url.
PageReference pageRef = Page.pagename; //enter your pagename here.
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('Id', a.Id); //setup id
- setup controlller with the record
ApexPages.StandardSetController stdController = new ApexPages.StandardSetController(a); //use the record
- initialize the class
passengerExtention qCls = new passengerExtention(stdController);
--- Once this works, you still need to follow the best practice of tetsing where you create and run as test user plus other possitive negative cases for the class and exceptions.
I hope this helps.
thanks
Shashi
Thank you very much for your assistance. I have created the test below with the help of your instructions, but I get the error:
Compile Error: Constructor not defined: [ApexPages.StandardSetController].<Constructor>(Individuele_Boeking__c) at line 17 column 58
@istest
public class createNewPassengerOnBookingTest
{
static testmethod void TriggerTest()
{
Account a = TestDataFactory.createAccount();
Contact c = TestDataFactory.createContact(a);
Contact c2 = TestDataFactory.createContact2(a);
Ship__c s = TestDataFactory.createSchip();
Reis__c r = TestDataFactory.createReis(s);
Ships_location__c bt = TestDataFactory.createBerth(s);
Individuele_Boeking__c b = TestDataFactory.createBoekingPlus(r, c, c2, bt);
PageReference pageRef = Page.New_Passenger_on_Booking;
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('Id', b.Id);
ApexPages.StandardSetController stdController = new ApexPages.StandardSetController(b);
passengerExtention qCls = new passengerExtention(stdController);
}
}
try changing
ApexPages.StandardSetController stdController = new ApexPages.StandardSetController(b);
to
ApexPages.StandardController stdController = new ApexPages.StandardController(b);
thanks
shashi
The coverage is only two lines but i'll work on it.
Best regards, Robert