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
Francesca GiroldiniFrancesca Giroldini 

test simple query class method

Hi at all, 
I'm trying to write a test class for a very simple query class, but I receive the error invalid constructor. 
Can anyone help me.
Thank you
public class ClosedTaskListController {
  @AuraEnabled
  public static List<Task> getTasks(ID AccountId) {
    Account a = [Select PersonContactId From Account Where Id = :AccountId];
    return [SELECT Id, Subject, Status, CreatedDate, Owner.Name, Description, Type 
    FROM Task WHERE isClosed=True AND WhoId=:a.PersonContactId ORDER BY createdDate DESC];
  }
    
}

 
Anil SomasundaranAnil Somasundaran
Hi,

Please try this, I think it won't have the above-mentioned issue.
@isTest
public class ClosedTaskListControllerTest {

    static testmethod void getTasksTest() {
        ClosedTaskListController.getTasks('<Valid AccountId>');
    }
}

Thanks,
Anil
Please mark this as best answer and upvote if your query has been resolved. Visit my blog to find more about lightning https://techevangel.com/author/anilsomasundaran/
 
Waqar Hussain SFWaqar Hussain SF
Hi Frans,

Please try the folowing test class.
@isTest
public class ClosedTaskListController_Test {

    private static testmethod void getTasksTest() {
		//creating test data
		String recordTypeId  = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
        Account acc= new Account(
          RecordTypeID=recordTypeId ,
          FirstName='Test FName',
          LastName='Test LName',
          PersonMailingStreet='test@yahoo.com',
          PersonMailingPostalCode='12345',
          PersonMailingCity='SFO',
          PersonEmail='test@yahoo.com',
          PersonHomePhone='1234567',
          PersonMobilePhone='12345678' 
        );
 
        insert acc;
		
		Task tsk = new Task ();
		tsk.Subject = 'Test Task';
		tsk.Status = 'New';
		tsk.ActivityDate = date.today();
		tsk.whoId = acc.PersonContactId;
		tsk.WhatId = acc.Id;
		tsk.Type = 'Call';
		insert tsk;
		
		test.startTest();
			List<Task> tasks = ClosedTaskListController.getTasks(acc.Id);
		test.StopTest();
    }
}

Let me know If you have any concern. 

Thanks