+ Start a Discussion
tmbarrytmbarry 

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.  

Best Answer chosen by Admin (Salesforce Developers) 
SRajesh8SRajesh8

Hi

 

Try This

 

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 
SD_Member__c sd = New SD_Member__c(First_Name__c = 'John', Last_Name__c = 'Smith', Ins_ID_Member__c = '123456', Ins_ID_Suscriber__c = '456789',
                                   address__c = '123 Main Stret', city__c = 'Aster', state__c = 'CT', zip__c = '06001', name = 'John Smith');

ApexPages.standardController stdController = new ApexPages.standardController(sd);
SDTasks  MyController = new SDTasks(stdController);

//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.
List <Task> abc = new List <Task>();
//here insesrt the Task abc
abc=MyController.getOpenTasks();
List<Task> abc1= new List<Task>();
//Here insert one more Task() abc1
abc1=MyController.getClosedTasks();
} }

 

Regards,

Rajesh.

All Answers

colemabcolemab

Since this is a controller extension, you can't just create a new object.  Here is the problem code:

SDTasks t = new SDTasks();

 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:

// Create an instance of the lead object with required fields
Lead TestObjLead = new lead(Status = 'Open', LastName = 'Test', Company = 'Test Inc.');
		
// Test the Creation the std controller which is required to create the extension
ApexPages.StandardController stdController = new ApexPages.StandardController(TestObjLead);

// Test the Creation of the extension by passing the std controller
MYControllerExtension MyController = new MYControllerExtension(stdController);

 

Hope this helps.

tmbarrytmbarry

Ths for the help. 

 

I updated my test class as follows

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 
SD_Member__c sd = New SD_Member__c(First_Name__c = 'John', Last_Name__c = 'Smith', Ins_ID_Member__c = '123456', Ins_ID_Suscriber__c = '456789',
                                   address__c = '123 Main Stret', city__c = 'Aster', state__c = 'CT', zip__c = '06001', name = 'John Smith');

ApexPages.standardController stdController = new ApexPages.standardController(sd);

MYControllerExtension MyController = new MYControllerExtension(stdController);

//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();
}
}

 But not I am getting a Error: Compile Error: Invalid type: MYControllerExtension at line 14 column 42

MYControllerExtension MyController = new MYControllerExtension(stdController);

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. 

colemabcolemab

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:

 

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 
SD_Member__c sd = New SD_Member__c(First_Name__c = 'John', Last_Name__c = 'Smith', Ins_ID_Member__c = '123456', Ins_ID_Suscriber__c = '456789',
                                   address__c = '123 Main Stret', city__c = 'Aster', state__c = 'CT', zip__c = '06001', name = 'John Smith');

ApexPages.standardController stdController = new ApexPages.standardController(sd);

SDTasks  MyController = new SDTasks(stdController);

//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.
SD_Member__c tTask = MyController.getSD_Member__c();
//test when type == null
MyController.viewSD_Member__c();
}
}

 

Let me know if this works.

tmbarrytmbarry

Unfortunately I can only get this far:

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 
SD_Member__c sd = New SD_Member__c(First_Name__c = 'John', Last_Name__c = 'Smith', Ins_ID_Member__c = '123456', Ins_ID_Suscriber__c = '456789',
                                   address__c = '123 Main Stret', city__c = 'Aster', state__c = 'CT', zip__c = '06001', name = 'John Smith');

ApexPages.standardController stdController = new ApexPages.standardController(sd);
SDTasks  MyController = new SDTasks(stdController);

//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.

}
}

 After that, I am still lost.  When I press the "Run Test" button, I only get Code Coverage Total % 33


  public class SDTasks {
 2  
 3   public SDTasks (ApexPages.StandardController controller) {
 4   }
 5  
 6   public PageReference edit() {
 7   return null;
 8   }
 9  
 10  List<Task> OpenTasks;
 11  List<Task> ClosedTasks;
 12  
 13  Public List<Task> getOpenTasks () {
 14  
 15   Return ([select subject, Status, Priority, ActivityDate, whatid, id
 16   From Task
 17   Where whatid = :system.currentPageReference().getParameters().get('id')
 18   And IsClosed = False]);
 19  }
 20  
 21  Public List<Task> getClosedTasks () {
 22  
 23   Return ([select subject, Status, Priority, ActivityDate, whatid, id
 24   From Task
 25   Where whatid = :system.currentPageReference().getParameters().get('id')
 26   And IsClosed = True
 27   Order by ActivityDate desc
 28   Limit 5]);
 29  }
 30  }

 

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! 

colemabcolemab

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 ...

SRajesh8SRajesh8

Hi

 

Try This

 

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 
SD_Member__c sd = New SD_Member__c(First_Name__c = 'John', Last_Name__c = 'Smith', Ins_ID_Member__c = '123456', Ins_ID_Suscriber__c = '456789',
                                   address__c = '123 Main Stret', city__c = 'Aster', state__c = 'CT', zip__c = '06001', name = 'John Smith');

ApexPages.standardController stdController = new ApexPages.standardController(sd);
SDTasks  MyController = new SDTasks(stdController);

//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.
List <Task> abc = new List <Task>();
//here insesrt the Task abc
abc=MyController.getOpenTasks();
List<Task> abc1= new List<Task>();
//Here insert one more Task() abc1
abc1=MyController.getClosedTasks();
} }

 

Regards,

Rajesh.

This was selected as the best answer
tmbarrytmbarry

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

   public PageReference edit() {
        return null;
    }

 Any thoughts on that one?

colemabcolemab

try adding this line to your test method to test that function call:

 

PageReference NullPageReference = MyController.edit();

 

 

tmbarrytmbarry

That did it ColeMab!!!!  100% coverage!

 

Thank you for all your help!

colemabcolemab

any time :smileyhappy: