You need to sign in to do that
Don't have an account?
Unit Testing Controller Extensions
I'm getting an error in trying to write a Test method that will compile. I started with the sample test code from the VF Developer's Guide. Here's my code:
public class newIP_Extension{
private final IP__c ip;
public newIP_Extension(ApexPages.StandardController stdController){
this.ip = (IP__c)stdController.getRecord();
}
<SNIP - many methods>
public static testMethod void test_newIP_Extension(){
Test.setCurrentPageReference(new PageReference('Page.newIP'));
newIP_Extension controller = new newIP_Extension('IP__c');
String nextPage = controller.continueToNext().getUrl();
<SNIP various tests>
}
}
This is the error I get "Error: Compile Error: Constructor not defined: [newIP_Extension].<Constructor>(String) at line 148 column 38 ."
If I don't pass 'IP__c' (the name of the standard controller) into the constructor I get "Error: Compile Error: Constructor not defined: [newIP_Extension].<Constructor>() at line 148 column 38 ."
If I don't quote IP__c I get "Error: Compile Error: Variable does not exist: IP__c line 148 column 58. "
Thanks for any insights!
The limitation is that the standardController can not be constructed.
The workaround is to create a new constructor that takes an sobject like the one handed to your extension in the required constructor. Then minimize the amount of logic in your constructor by factoring it out into a method that can be called from both constructors so you can cover as much code as possible with your test.
Here is a quick sample for you....
All Answers
The limitation is that the standardController can not be constructed.
The workaround is to create a new constructor that takes an sobject like the one handed to your extension in the required constructor. Then minimize the amount of logic in your constructor by factoring it out into a method that can be called from both constructors so you can cover as much code as possible with your test.
Here is a quick sample for you....
If I can't create an instance of a standardController, how can I execute this code in a test method?
Many thanks
Sam
Of course if you already have a task, don't create a new one ;-)
Many thanks for the quick response. I have reworked the code (back to how I had it before I found this post ;)) and I'm now getting the same internal server error that originally I thought was being cause by the standardController constructor issue (the error occurs when I run the tests). My testmethod is below, the error page is beneath that.
It seems that the error is caused by the TestExtension.SaveAndUpdateStatus() function call (shown in red above). If I comment this out, the tests run with no failures.
Sam
Message Edited by samd on 07-25-2008 03:50 AM
Message Edited by samd on 07-25-2008 03:55 AM
Many thanks
Sam
Sam