You need to sign in to do that
Don't have an account?
DNLZ
Test class for controller extension
Hi,
I've got a question relating to a controller extension i made for a visualforce page. The goal was to show related activities(Events and Tasks) from child objects on the master object detail page. This worked out all good, but i was wondering is there a best practise for writing tests?
This is the code for the controller extension. I obscured my customObjects so i know about the weird custom object names :)
public class getRelatedActivities { private List<Event> events; private List<Task> tasks; private Opportunity opportunity; public getRelatedActivities(ApexPages.StandardController controller) { this.opportunity = (Opportunity)controller.getRecord(); } public List<Event> getEvents() { List<Aobj__c> aObjecten = new List<Aobj__c>{}; aObjecten = [Select Id from Aobj__c WHERE Zopd__c = :opportunity.id]; List<Id> aObjectIds = new List<Id>{}; for(Aobj__c aobj: aObjecten){ aObjectIds.add(aobj.id); } events = [SELECT Id, Location, StartDateTime, Subject, EndDateTime FROM Event WHERE WhatId IN :aObjectIds AND EndDateTime > TODAY]; return events; } }
So what is the best practise of testing this?
Thanks
Hi,
You have to simply crate the instance of the class inside the test method and call the method through the instance of you class. Try the below code as reference(Made changes accordingly):
public class getRelatedActivities
{
private List<Event> events;
private List<Task> tasks;
private Opportunity opportunity;
public getRelatedActivities(ApexPages.StandardController controller) {
this.opportunity = (Opportunity)controller.getRecord();
}
public List<Event> getEvents() {
List<Aobj__c> aObjecten = new List<Aobj__c>{};
aObjecten = [Select Id from Aobj__c WHERE Zopd__c = :opportunity.id];
List<Id> aObjectIds = new List<Id>{};
for(Aobj__c aobj: aObjecten){
aObjectIds.add(aobj.id);
}
events = [SELECT Id, Location, StartDateTime, Subject, EndDateTime FROM Event WHERE WhatId IN :aObjectIds AND EndDateTime > TODAY];
return events;
}
static testmethod void aboveClass()
{
opportunity op=new opportunity(name='test', other mnadatory fields);
insert op;
getRelatedActivities g=new getRelatedActivities(op);
g.getEvents();
}
}
For more details please go through the link below:
http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
All Answers
Hi,
You have to simply crate the instance of the class inside the test method and call the method through the instance of you class. Try the below code as reference(Made changes accordingly):
public class getRelatedActivities
{
private List<Event> events;
private List<Task> tasks;
private Opportunity opportunity;
public getRelatedActivities(ApexPages.StandardController controller) {
this.opportunity = (Opportunity)controller.getRecord();
}
public List<Event> getEvents() {
List<Aobj__c> aObjecten = new List<Aobj__c>{};
aObjecten = [Select Id from Aobj__c WHERE Zopd__c = :opportunity.id];
List<Id> aObjectIds = new List<Id>{};
for(Aobj__c aobj: aObjecten){
aObjectIds.add(aobj.id);
}
events = [SELECT Id, Location, StartDateTime, Subject, EndDateTime FROM Event WHERE WhatId IN :aObjectIds AND EndDateTime > TODAY];
return events;
}
static testmethod void aboveClass()
{
opportunity op=new opportunity(name='test', other mnadatory fields);
insert op;
getRelatedActivities g=new getRelatedActivities(op);
g.getEvents();
}
}
For more details please go through the link below:
http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
So i'll need to create a test method in the controller extension class? Is this a best practise? And is this also a best practise for triggers?
Thanks for your answer!
Hi,
For class I would suggest to write the test method inside the class itself. For trigger create a new class for the test method.
Thanks for your help :)