You need to sign in to do that
Don't have an account?
tmbarry
Unit Testing Controller Extensions Error Message
I created a controller to pull both Open and Closed task for a custom object I built. The controller code is:
public class SDTasks { public SDTasks (ApexPages.StandardController controller) { } public PageReference edit() { return null; } private User user; private boolean isEdit = false; public User getUser() { return user;} List<Task> OpenTasks; List<Task> ClosedTasks; Public List<Task> getOpenTasks () { Return ([select subject, Status, Priority, ActivityDate, whatid, id From Task Where whatid = :system.currentPageReference().getParameters().get('id') And IsClosed = False]); } Public List<Task> getClosedTasks () { Return ([select subject, Status, Priority, ActivityDate, whatid, id From Task Where whatid = :system.currentPageReference().getParameters().get('id') And IsClosed = True Order by ActivityDate desc Limit 5]); } Public String getName() { Return 'Get Open Task'; } public string SdMemberId {get;set;} }
Now I am trying to create the test class and receive this error: Error: Compile Error: Constructor not defined: [SDTasks].<Constructor>() at line 8 column 13
My test class code is
public class testSDTasks { static testMethod void myPage_Test() { //Test converage for the Page.ToDo_ControlPanel_V8 visualforce page PageReference pageRef = Page.ToDo_ControlPanel_V8; Test.setCurrentPageReference(pageRef); // create an instance of the controller SDTasks t = new SDTasks(); //try calling methods/properties of the controller in all possible scenarios // to get the best coverage. SD_Member__c tTask = t.getSD_Member__c(); //test when type == null t.viewSD_Member__c(); } }
Since I am new to this, I am not sure how to proceed? Any help would be appreciated.
Hi
Try This
Regards,
Rajesh.
All Answers
Since this is a controller extension, you can't just create a new object. Here is the problem code:
I am not sure what your custom object is named, but here is some code to create an object from an extension on the lead object:
Hope this helps.
Ths for the help.
I updated my test class as follows
But not I am getting a Error: Compile Error: Invalid type: MYControllerExtension at line 14 column 42
As a little background, I designed VF page to open a new view of the SD_Member__c ojbect by pressing a button on the SD_Member__c standard page layout. The VF opens the current SD_Member__c record and then uses the controller I built to view the the Open and Closed tasks for that SD_Member__c .
Thanks again for the help.
Todd,
I was just listing my code as an example and of course the class was different. Not sure it will work in your system but I modified it here to better match your code:
Let me know if this works.
Unfortunately I can only get this far:
After that, I am still lost. When I press the "Run Test" button, I only get Code Coverage Total % 33
public class SDTasks {
I guess I still need to add code to check the getOpenTasks and get ClosedTasks part of the controller.
Who knew the Test Class would be the hardest part of this project!
Geting test coverage is the hardest part when you first program in apex but it doest get easier with experience. Getting good test logic (i.e. with asserts) is what you will eventually be shooting for and that takes some planning.
If you haven't already, you need to read about bulkifying your code for triggers before writing one or you will hit limits when you least expect it :)
Anyway, reply to this thread if you need more help on this class ...
Hi
Try This
Regards,
Rajesh.
Rajesh,
That helps out A LOT thank you. Now when i run the Tests, I get 77% coverage. The only thing it's says I am not testing is this line of code
Any thoughts on that one?
try adding this line to your test method to test that function call:
That did it ColeMab!!!! 100% coverage!
Thank you for all your help!
any time :smileyhappy: