+ Start a Discussion
Michael Degn JensenMichael Degn Jensen 

Apex Class unit test get list of records + update records

Hi,

Amost there with unit testing of my first Aura Component, however I am stuck with testing of 2 functions. The first finds all the tasks assigned to current user and the second updates a task to Status=Completed.

First function:
@AuraEnabled
    public static List<Task> getTasks(Id oUser){
        System.debug('oUser'+oUser);
       List<Task> taskList = [SELECT Id,Subject,Description,OwnerId,ActivityDate,Who.Name,Priority,Status,Type,AL_Assigned_To__c
                              FROM Task
                              WHERE OwnerId=:oUser AND ActivityDate <= NEXT_N_DAYS:3 AND Status != 'Completed'
                              ORDER BY ActivityDate ASC
                              LIMIT 7];
       System.debug('taskList: ' + taskList);
        return taskList;
    }
Second function:
@AuraEnabled
    public static void updateDetails(List<String> lstRecordId) {
        List<Task> lstUpdate = new List<Task>();
        for(Task t : [SELECT Id, Status FROM Task WHERE Id IN : lstRecordId]){
            t.Status = 'Completed'; // fields to update
            lstUpdate.add(t);
        }
        if(lstUpdate.size() > 0){
            update lstUpdate;
        }
    }

I have done the Trailhead Apex Testing (https://trailhead.salesforce.com/content/learn/modules/apex_testing?trail_id=force_com_dev_beginner) but maybe I'm not getting it or it doesn't cover what I'm trying to test here.

Here is where I'm at in the first test:
@isTest static void testGetTasks() {
        Task taskt = new Task(Subject='Test Task', OwnerId=UserInfo.getUserId(), Status='Not Started');
        insert taskt;

    }

Any help is really appreciated.

Thanks,
Michael
Best Answer chosen by Michael Degn Jensen
Santosh Kumar 348Santosh Kumar 348
Hi Michael,

Try below code if it works :
@isTest
public class ALTaskControllerTest{
    static testmethod void testGetTasks() {
        Task t = new Task();
        t.Subject = 'Test Task';
        t.OwnerId = UserInfo.getUserId();
        t.Status = 'Not Started';
        insert t;
        
        List<Task> lstTask= ALTaskController.getTasks(UserInfo.getUserId()); 
        System.assert(lstTask != null);
    }
    static testmethod void testupdateDetails() {
        List<Task> taskTobeUpdated = new List<Task>();
        for(integer i=0; i<3; i++){
            Task t = new Task();
            t.Subject = 'Test Task' +i;
            t.OwnerId = UserInfo.getUserId();
            t.Status = 'Not Started';
            taskTobeUpdated.add(t);
        }
        
        insert taskTobeUpdated;
        List<String> recordIdtobeUpdated = new List<String>();
        for(Task taskIns : taskTobeUpdated){
            recordIdtobeUpdated.add(taskIns.id);
        }
        ALTaskController.updateDetails(recordIdtobeUpdated); 
    }
}

Do let me know if it helps you and close your query by marking it as solved.

Thanks,
Santosh Kumar

All Answers

DimondDimond
Call the methods after your insert statement and see what happens. classname.methodname
Michael Degn JensenMichael Degn Jensen
Got this going for the first test, but getting this error "Method does not exist or incorrect signature: void getTasks() from the type ALTaskController"
@isTest static void testGetTasks() {
        Task t = new Task();
        t.Subject = 'Test Task';
        t.OwnerId = UserInfo.getUserId();
        t.Status = 'Not Started';
        insert t;
        
        ALTaskController testCont = new ALTaskController();
        List<Task> lstTask = testCont.getTasks();
        
        System.assert(lstTask != null);
    }

Then guessing that it might need the parameter Id oUser, so tried to add "UserInfo.getUserId()" to line 9:
List<Task> lstTask = testCont.getTasks(UserInfo.getUserId());

But then getting this error instead "Static method cannot be referenced from a non static context: List<Task> ALTaskController.getTasks(Id)"

I know that there is something very basic about unit testing that I am missing here, but any pointer in the right direction is appreciated!

Thanks,
Michael

Deepali KulshresthaDeepali Kulshrestha
Hi Michael,

Try the following test class it may be helpful for you:
​@isTest
public class testGetClass {
@isTest static void testGetClass() {
        Task t = new Task();
        t.Subject = 'Test Task';
        t.OwnerId = UserInfo.getUserId();
        t.Status = 'Not Started';
        insert t;
        
        ALTaskController testCont = new ALTaskController();
        List<Task> lstTask = testCont.getTasks();
        
        List<String> lstRecordId=new List<String>();
        lstRecordId.add(t.id);
        system.debug('lstRecordId---'+lstRecordId);
        testCont.updateDetails(t.id);
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Michael Degn JensenMichael Degn Jensen
Hi Deepali

Thanks for your reply.

This code give med 2 errors:
Line 11: "Method does not exist or incorrect signature: void getTasks() from the type ALTaskController"
Line 16: "Method does not exist or incorrect signature: void updateDetails(Id) from the type ALTaskController"
@isTest
private class ALTaskControllerTest {   
    @isTest static void testGetTasks() {
        Task t = new Task();
        t.Subject = 'Test Task';
        t.OwnerId = UserInfo.getUserId();
        t.Status = 'Not Started';
        insert t;
        
        ALTaskController testCont = new ALTaskController();
        List<Task> lstTask = testCont.getTasks();
        
        List<String> lstRecordId = new List<String>();
        lstRecordId.add(t.Id);
        System.debug('lstRecordId---' + lstRecordId);
        testCont.updateDetails(t.Id);
    }
}
Also I think the 2 Apex Class functions are mixed together here (updateDetails and getTasks)?

Thanks,
Michael
 
Santosh Kumar 348Santosh Kumar 348
Hi Michael,

Try below code if it works :
@isTest
public class ALTaskControllerTest{
    static testmethod void testGetTasks() {
        Task t = new Task();
        t.Subject = 'Test Task';
        t.OwnerId = UserInfo.getUserId();
        t.Status = 'Not Started';
        insert t;
        
        List<Task> lstTask= ALTaskController.getTasks(UserInfo.getUserId()); 
        System.assert(lstTask != null);
    }
    static testmethod void testupdateDetails() {
        List<Task> taskTobeUpdated = new List<Task>();
        for(integer i=0; i<3; i++){
            Task t = new Task();
            t.Subject = 'Test Task' +i;
            t.OwnerId = UserInfo.getUserId();
            t.Status = 'Not Started';
            taskTobeUpdated.add(t);
        }
        
        insert taskTobeUpdated;
        List<String> recordIdtobeUpdated = new List<String>();
        for(Task taskIns : taskTobeUpdated){
            recordIdtobeUpdated.add(taskIns.id);
        }
        ALTaskController.updateDetails(recordIdtobeUpdated); 
    }
}

Do let me know if it helps you and close your query by marking it as solved.

Thanks,
Santosh Kumar
This was selected as the best answer
Michael Degn JensenMichael Degn Jensen
Yes @Santosh Kumar 348, that did the trick, and I learned a lot! Thank you so much :)
Thanks,
Michael
Deepali KulshresthaDeepali Kulshrestha
Hi Michael,

You don't call your function correctly. That's why this error occurs. So call your function just like this way: ClassName.FunctionName.
Try the following test class it may be helpful for you:
​@IsTest
private class ALTaskControllerTest {   
    @IsTest static void testGetTasks() {
        Task t = new Task();
        t.Subject = 'Test Task';
        t.OwnerId = UserInfo.getUserId();
        t.Status = 'Not Started';
        insert t;
        
        //First-Function call
        // ClassName.MethodName
        List<Task> lstTask = ALTaskController.getTasks();
        
        //Second-Function call
        // ClassName.MethodName
        List<String> lstRecordId = new List<String>();
        lstRecordId.add(t.Id);
        ALTaskController.updateDetails(t.Id);
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha