You need to sign in to do that
Don't have an account?
Test Class for controler - Routine feeding data to a VisualForce page generating a PDF Layout
Hi,
I've got a question relating to a controller extension i made for a visualforce page.
The goal is to supply the list of related object columns for generating a PDF layout using both the Opportunity fields and related object fields.
This works great, but I am now wondering how to write test classes for controller classes?
This is the summarised code for the controller extension.
public class Layout_LinkOppLines{ public List<Tender__c> OpplineTender{get;set;} public Layout_LinkOppLines(ApexPages.StandardController controller){ OpplineTender=[select Id, Linked_Project__c, Comments__c, Role__c, Tender_Win_Loss__c, Linked_Account__c from Tender__C where Linked_Project__c =:(ApexPages.CurrentPage().getParameters().get('id'))]; } }
How do I structure the Test Class to allow a good code coverage.
I have done a few test class for triggers, but none of Controller classes.
Looking at the developper help file did not bring me anywhere.
Thanks
Try this way,
static testMethod void myTest() {
PageReference pageRef = Page.Name_Of_the_VFPage;
Test.setCurrentPage(pageRef);
Opportunity op = [Select Id from Opportunity limit 1]; // if the standard controller is on Opportunity
ApexPages.Standardcontroller sc1 = new ApexPages.Standardcontroller(op);
System.CurrentPageReference().getParameters().put('Id', 'xxxxxxxx');
Layout_LinkOppLines inst = new Layout_LinkOppLines(sc1);
}
All Answers
Try this way,
static testMethod void myTest() {
PageReference pageRef = Page.Name_Of_the_VFPage;
Test.setCurrentPage(pageRef);
Opportunity op = [Select Id from Opportunity limit 1]; // if the standard controller is on Opportunity
ApexPages.Standardcontroller sc1 = new ApexPages.Standardcontroller(op);
System.CurrentPageReference().getParameters().put('Id', 'xxxxxxxx');
Layout_LinkOppLines inst = new Layout_LinkOppLines(sc1);
}
Hi,
Thanks a lot for that.
I have tried to copy the code on the bottom of my class. It gave me the error:
System.QueryException: List has no rows for assignment to SObject
After a quick search on the forum, I noticed that in API version 24 and later, all objects are set to 'Private' as default.
I have then created an other class as below and it works thank to the @IsTest(SeeAllData=True).
Is this good practice? Should I be in a position to use the flag in my main class?
System.CurrentPageReference().getParameters().put('Id', op.Id);
@IsTest(SeeAllData=True) is only used for test class, not for your main class.
Thanks a Lot. I have put the tag, and set the API version to 23 and the Test passes fine.