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
Developer2016Developer2016 

Test Class code coverage help

Hi Experts,
 
Please find below Class, Trigger, and Test Class.
At present with my test class, it cover class with 33% and Trigger 0% need test class code cover help.
Please anyone help me.
 

Apex Class:


public with sharing class TaskTriggerHandler {
 
    private boolean m_isExecuting = false;
    private integer BatchSize = 0;
 
    public TaskTriggerHandler(boolean isExecuting, integer size) {
        m_isExecuting = isExecuting;
        BatchSize = size;
    }
   
    public void OnAfterUpdate(Map<ID, Task> newRecordMap, Map<ID, Task> oldRecordMap) {
       
        //if one Task related to WorkflowInstance was closed, clase all others Tasks related to the WorkflowInstance
        set<String> workflowInstanceIds = new set<String>();
       
        Schema.DescribeSObjectResult result = Workflow_Instance__c.SObjectType.getDescribe();
       
        String prefix_WorkflowInstance = result.getKeyPrefix();
       
        for(Id newRecordId: newRecordMap.keySet()){
            Task newRecord = newRecordMap.get(newRecordId);
            Task oldRecord = oldRecordMap.get(newRecordId);
            if(newRecord.IsClosed && !oldRecord.IsClosed && newRecord.WhatId != null && String.valueOf( newRecord.WhatId ).substring(0,3) == prefix_WorkflowInstance ){
                workflowInstanceIds.add(newRecord.WhatId);
            }
        }
        if(workflowInstanceIds.size() > 0) WorkflowUtil.closeTasks( workflowInstanceIds );
       
    }
}
 
 
 
Trigger:
 
trigger TaskTrigger on Task (after update) {
    TaskTriggerHandler handler = new TaskTriggerHandler(Trigger.isExecuting, Trigger.size);
    if(Trigger.isUpdate && Trigger.isAfter)  handler.OnAfterUpdate(Trigger.newMap, Trigger.oldMap);
}
 


 
Test Class:
 
@isTest
private class TaskTriggerHandlerTest {
 
    @isTest
    private static void TaskTriggerHandlerTest(){
       
        User u = [select id,Primary_UBE_Id__c from User where id=:UserInfo.getUserId()];
        u.Primary_UBE_Id__c = '114';
        update u;
 
        boolean m_isExecuting = false;
       
        //m_isExecuting = isExecuting;
        //BatchSize = size;
 
        Test.startTest();
        Test.stopTest();
    }
   
    @isTest
    private static void OnAfterUpdateTest(){
       
        User u = [select id,Primary_UBE_Id__c from User where id=:UserInfo.getUserId()];
        u.Primary_UBE_Id__c = '114';
        update u;
      
        Request__c request = new Request__c(name = 'test');
        insert request;
       
        Workflow_Instance__c workflowInstance = new Workflow_Instance__c(Request__c = request.Id);
        insert workflowInstance;
       
        Task told = new Task(Ownerid = U.id, Subject = 'Test', Whatid = workflowInstance.id, Status = 'Pending', Priority = 'Low', Description = 'Test');
        insert told;
       
        Task tnew = new Task(Ownerid = U.id, Subject = 'Test', Whatid = workflowInstance.id, Status = 'Closed', Priority = 'Low', Description = 'Test');
        insert tnew;
        
        Map<ID, Task> newRecordMap = new map<ID, Task>();       
        Map<ID, Task> oldRecordMap = new map<ID, Task>();
       
        set<String> workflowInstanceIds = new set<String>();
       
        Test.startTest();
            TaskTriggerHandler handler = new TaskTriggerHandler(Trigger.isExecuting, Trigger.size);
            //if(Trigger.isUpdate && Trigger.isAfter)  handler.OnAfterUpdate(Trigger.newMap, Trigger.oldMap);
        Test.stopTest();
    }
 
}
 
AnupamAnupam
Hi

Why don't you just try to create update task between start and stop test in the second test method, instead of calling the trigger handler class and see if it works.

Thanks
Anupam
AshlekhAshlekh
Hi,

Could you please provide the code which you have written so far. So we can do help further. A new test class written for this code my community Member is a not a good approch.

-Thanks
Ashlekh Gera
Tarun J.Tarun J.
Hello,

Try this between Test.startTest() and Test.stopTest()
told.Status = 'Closed';
update told;

-Thanks,
TK
AnupamAnupam
@Developer2016, mark this post resolve if your problem is solved.