Creating Apex Test methods in Salesforce "constructor not defined"
I've written an apex class that executes when a user presses a button. It saves the current data to the 开发者_StackOverflow社区log, checks the page that they are currently on and reloads it (so they are displayed with a blank instance of the logger). I am currently trying to write the test method for this class and am getting this error: "Compile Error: Constructor not defined: [Logger_Extend].() at line 45 column 38" (line 45 is this one "Logger_Extend controller = new Logger_Extend();").
Here is the relevant code. Does anybody have any ideas on what I'm doing wrong?
public class Logger_Extend {
private final RCA_Logger__c Log;
public Logger_Extend (ApexPages.StandardController
stdController) {
Log = (RCA_Logger__c)stdController.getRecord();
}
public PageReference XX() {
// Add the account to the database.
insert Log;
// Send the user back to current page.
PageReference pageRef = ApexPages.currentPage();
pageRef.setRedirect(true);
return pageRef;
}
static testMethod void myTest() {
Logger_Extend controller = new Logger_Extend();
PageReference pageRef = ApexPages.currentPage();
System.assert(controller.XX() == pageRef);
}
}
You don't have a constructor for Logger_Extend that does not take any arguments, but you are trying to instantiate one in your test.
While an argument-less constructor is created for you by default, once you add a custom constructor that takes arguments, you need to also add an argument-less constructor.
From the Apex Developers reference:
If you write a constructor that takes arguments, you can then use that constructor to create an object using those arguments.
If you create a constructor that takes arguments, and you still want to use a no-argument constructor, you must include one in your code. Once you create a constructor for a class, you no longer have access to the default, no-argument public constructor. You must create your own.
精彩评论