function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VaderVader 

Apex Test Method Error: Constructor Not Defined?

So I've come to realize that there are two errors that I am learning to dislike very much...

1)  Easilly the one I dislike the most is:  "System.NullPointerException: Attempt to de-reference a null object"

2) The second has become:  Save error: Constructor not defined: [ApexPages.StandardController].<Constructor>(xxxxx)

 

I've just written a half dozen or more Test Classes using the same syntax for creating the constructor in my test method but for some reason, this one is giving me a fit.

 

This class is an extension class.  Is there something I need to do different? The constructors in all my classes are set up almost identically so I would assume creating them in my test class would be as well.

 

public with sharing class taskMyDelegatedView {
	
	public Id tskId {get;set;}	
	public Id uID {get;set;}
	public String iView {get;set;}
	
	
	private ApexPages.StandardController controller;
	public taskMyDelegatedView(ApexPages.StandardController stdController) {
      controller = stdController;
   }
	
	Id currentUserId = userinfo.getUserId();	
	   
	public List<Task> getMyDelegatedTasks() {
        return [SELECT id, Subject, Description, Status, WhatId, What.Name, Owner.Name, OwnerId, CreatedBy.Name, CreatedById, ActivityDate, Task_Due_Date__c, LastModifiedDate FROM Task WHERE CreatedById =: currentUserId ORDER BY LastModifiedDate];        
    }
    
   
   String result='Click on a task subject to preview the latest notes.';
   public String getFetchedData() {
   		return result;
   } 
   
   public PageReference invokeService() {
   		Id id = System.currentPageReference().getParameters().get('id');
   		result = [SELECT Description FROM Task WHERE Id=:id].Description;
   		return null;   	
   }
   

    public static Task testTsk;
    public static taskMyDelegatedView testTaskMDV;
    public static Task_Informed_Users__c testInformedUser;
    public static User testUserId;
        
    static testMethod void informedUserListTest() {    	
    	Test.startTest();    		
    		
	    	testUserId = [SELECT ID FROM User WHERE LastName=: 'User2'];
	    	System.Debug(testUserId);
	    	
	        testTsk = new Task(Subject='testTask', description='ABC', ownerId=testUserId.Id); 
	        insert testTsk;
	        System.Debug(testTsk.Id);	        
	       	        
	            
	        testInformedUser = new Task_Informed_Users__c(Informed_user__c=testUserId.Id, Task_ID__c=testTsk.Id, Chatter_Notification__c=FALSE, Send_Email__c=FALSE);
	        insert testInformedUser;
	        System.Debug(testInformedUser.Id);

	        
	        Test.setCurrentPage(Page.delegatedTaskView);
		ApexPages.currentPage().getParameters().put('Id',testTsk.Id);
			
		ApexPages.Standardcontroller con = new ApexPages.Standardcontroller(testTaskMDV); //ERROR HAPPENING HERE
		taskMyDelegatedView tTMDV = new taskMyDelegatedView(con);
	        
	        system.runas(testUserId) {
				

		}
        
        Test.stopTest();        
    }


}

 

Best Answer chosen by Admin (Salesforce Developers) 
VaderVader

I found the solution.  It was the way I was setting up my constructor in my test method for an extension that was incorrect.  

 

1) I created a static variable:  

public static Task testTsk;

2) I created a record in conjunction with that object variable:  

testTsk = new Task(Subject='testTask', description='ABC', ownerId=testUserId.Id); 

insert testTsk;

3) I instantiated the controller using the Task object (since my VF page used that as the standard controller):  

ApexPages.StandardController con = new ApexPages.StandardController(testTsk);

taskMyDelegatedView stdController = new taskMyDelegatedView(con);

 

 

This got me squared away and it's working fine now.  Test method below.

 

public static Task testTsk;
    public static Task_Informed_Users__c testInformedUser;
    public static User testUserId;    
        
    static testMethod void informedUserListTest() {    	
    	Test.startTest();    		
    		//Find a User ID to set as the current user
	    	testUserId = [SELECT ID FROM User WHERE LastName=: 'User2'];
	    	System.Debug(testUserId);
	    	
	    	//Create a Task that is created by and assigned to the user
	        testTsk = new Task(Subject='testTask', description='ABC', ownerId=testUserId.Id); 
	        insert testTsk;
	        System.Debug(testTsk.Id);	               
	        
	        //Insert an informed user record with the user queried above as the informed user
	        testInformedUser = new Task_Informed_Users__c(Informed_user__c=testUserId.Id, Task_ID__c=testTsk.Id, Chatter_Notification__c=FALSE, Send_Email__c=FALSE);
	        insert testInformedUser;
	        System.Debug(testInformedUser.Id);			
			
			//Instantiate the controller
			ApexPages.StandardController con = new ApexPages.StandardController(testTsk);
			taskMyDelegatedView stdController = new taskMyDelegatedView(con);
			system.assert(stdController != null);
			
			
			//Run the set of test code as the queried user	        
	        system.runas(testUserId) {
				//stdController.currentUserId = testUserId;
				
				stdController.getMyDelegatedTasks();
			}
        
        Test.stopTest();        
    }

 

All Answers

Sandeep001Sandeep001

Constructor for standard controller is defined as - 

ApexPages.StandardController sc = new ApexPages.StandardController(sObject);

 

 The testTaskMDV you are passing in standard controller is not a sObject - 
    public static taskMyDelegatedView testTaskMDV;

  

 

 
VaderVader
So to test my class methods should I use something like: taskMyDelegatedView.testTaskMDV.getFetchedData(); ?

Even though ID's are returning in my system.debug statements, I keep getting System.NullPointerException: Attempt to de-reference a null object errors when I do it this way.
MoggyMoggy

I am not 100% sure but don't you need a whatId to have a task created, otherwise the task doesn;t know to which object it is related ( nullpointer )

 

Beside that , where do you assign a value to testtaskMDV

I see its a public var but can't find the value assignment

nope, on a second view, your testtaskMDV used by the Apex standardcontroller is null

ApexPages.Standardcontroller con = new ApexPages.Standardcontroller(testTaskMDV)
VaderVader

Thanks for the response Moggy.  I am just generating a standalone task in the test code, not really setting it up to be related to any particular record so I didn't use the whoid or whatid values.

 

I see what you're saying about the null constructor now.  All this controller is supposed to do is provide a list of records for view on a specific visualforce page.  I think I am getting turned around on how to actually produce the test method for this class altogether...

 

 

VaderVader

I found the solution.  It was the way I was setting up my constructor in my test method for an extension that was incorrect.  

 

1) I created a static variable:  

public static Task testTsk;

2) I created a record in conjunction with that object variable:  

testTsk = new Task(Subject='testTask', description='ABC', ownerId=testUserId.Id); 

insert testTsk;

3) I instantiated the controller using the Task object (since my VF page used that as the standard controller):  

ApexPages.StandardController con = new ApexPages.StandardController(testTsk);

taskMyDelegatedView stdController = new taskMyDelegatedView(con);

 

 

This got me squared away and it's working fine now.  Test method below.

 

public static Task testTsk;
    public static Task_Informed_Users__c testInformedUser;
    public static User testUserId;    
        
    static testMethod void informedUserListTest() {    	
    	Test.startTest();    		
    		//Find a User ID to set as the current user
	    	testUserId = [SELECT ID FROM User WHERE LastName=: 'User2'];
	    	System.Debug(testUserId);
	    	
	    	//Create a Task that is created by and assigned to the user
	        testTsk = new Task(Subject='testTask', description='ABC', ownerId=testUserId.Id); 
	        insert testTsk;
	        System.Debug(testTsk.Id);	               
	        
	        //Insert an informed user record with the user queried above as the informed user
	        testInformedUser = new Task_Informed_Users__c(Informed_user__c=testUserId.Id, Task_ID__c=testTsk.Id, Chatter_Notification__c=FALSE, Send_Email__c=FALSE);
	        insert testInformedUser;
	        System.Debug(testInformedUser.Id);			
			
			//Instantiate the controller
			ApexPages.StandardController con = new ApexPages.StandardController(testTsk);
			taskMyDelegatedView stdController = new taskMyDelegatedView(con);
			system.assert(stdController != null);
			
			
			//Run the set of test code as the queried user	        
	        system.runas(testUserId) {
				//stdController.currentUserId = testUserId;
				
				stdController.getMyDelegatedTasks();
			}
        
        Test.stopTest();        
    }

 

This was selected as the best answer