0

I have the following method implementation:

public void updateTaskCommentHistory(Task oldTask, Task newTask) {
    if(oldTask == Null || newTask == Null) {
        return;
    }
    String oldValue = oldTask.Description;
    String newValue = newTask.Description;
    // add new history record for Task only if the user has changed the description
    if(oldValue != newValue) {
        String taskId = newTask.Id;
        String lastModifiedBy = newTask.lastModifiedById;
        instertTaskActivityHistory(taskId, oldValue, newValue, fieldName, lastModifiedBy);
    }  
}

I also have the following test method:

static testMethod void testTask() {
        Test.startTest();

        TaskGateway gateway = new TaskGateway();

        List<Task> listOfTasks = testDataFactory.createTestActivityTasks(2);
        Task taskOldValue = listOfTasks.get(0);
        taskOldValue.Description = 'Old Comment';
        Task taskNewValue = listOfTasks.get(1);
        taskNewValue.Description = 'New Comment';
        gateway.updateTaskCommentHistory(taskOldValue, taskNewValue);           

            update listOfTasks;

            delete listOfTasks;

        Test.stopTest();
    }

I would like to know how to add different task descriptions in my test testTask(); method in order to be able to cover the if statement:

    if(oldValue != newValue) {
                String taskId = newTask.Id;
                String lastModifiedBy = newTask.lastModifiedById;
                instertTaskActivityHistory(taskId, oldValue, newValue, fieldName, lastModifiedBy);
}

in method updateTaskCommentHistory(task, task);

1 Answer 1

0

I assume your updateTaskCommentHistory method is being called from Task trigger / handler on Update event..

In your test method, insert a Task with either an empty description or a value like 'Description one' [testDataFactory.createTestActivityTasks(2) must already be inserting the tasks] ..

you don't need to explicitly call the gateway.updateTaskCommentHistory method in your test class..

if you update the tasks previously inserted by changing the Description field value to something else other than the initial value (e.g 'Description two')..

This will trigger the Task Update trigger and in turn will run the method with different description values in the new & old task..

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .