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
@23@23 

apex test code coverage

Issue with apex test class coverage
Trying to get code coverage for below class but even after trying several options, not achieving code coverage. After debugging the code, test data is created.

public class TaskBeforeDeleteController implements TriggerAction.BeforeDelete{
    public static void beforeDelete(List<Task> task) {        
        task = [SELECT Id, Activity_ID__c, WhatId FROM Task WHERE Id In : Trigger.old];
        //system.debug('task'+task);
        
        List<Activity__c> pAct = [SELECT Id, Activity_Id__c, Contract_File__c FROM Activity__c]; 
        //system.debug('pAct'+pAct);
        
        for(Task t : task) {
            Task actualRecord = (Task)Trigger.oldMap.get(t.Id);
            for(Activity__c pa : pAct) {
               if(t.Activity_ID__c != null && t.Activity_ID__c == pa.Activity_Id__c) {
                   //system.debug('inside IF');
                    actualRecord.adderror('Cannot delete Task record since Provision Activity is present');
                } 
            }
            
        }
    }
}

 

Test class

@isTest
public with sharing class TaskBeforeDeleteTest {
    @TestSetup
    public static void TestDataSetup() {
       //required test data is created
    }
static testmethod void testTaskBeforeDeleteController1() {
        //Test.startTest();
        Contract__c con = [SELECT Id FROM Contract__c];
        Activity__c act = [SELECT Id, Name, Activity_Id__c FROM Activity__c WHERE Contract_File__c =: con.Id LIMIT 1];
        List<Activity__c> proActList = [SELECT Id, Name, Activity_Id__c FROM Activity__c WHERE Description__c = 'Provision'];
        Task task = [SELECT Id, Activity_ID__c FROM Task WHERE Activity_ID__c =: act.Activity_Id__c LIMIT 1];
        List<Task> taskList = [SELECT Id, Activity_ID__c FROM Task WHERE Subject = 'Provision' AND Activity_ID__c != null];
        system.debug('con'+con);
        system.debug('act'+act);
        system.debug('proActList'+proActList);
        system.debug('task'+task);
        system.debug('taskList'+taskList);
        try {
            Test.startTest();        
            TaskBeforeDeleteController.beforeDelete(taskList);
            for(Task t : taskList) {
                TaskBeforeDeleteController.beforeDelete(taskList);
                for(Activity__c proA : proActList) {
                    if(t.Activity_ID__c != null && t.Activity_ID__c == proA.Activity_Id__c) {                        
                        System.assertNotEquals(task.Activity_ID__c != null, null, 'Task Activity ID is not null');
                        System.assertEquals(task.Activity_ID__c, act.Activity_Id__c, 'Task Activity and Provision Activity ID are same');
                    }
                }           
            }
        }
        catch(Exception e){
            Boolean message =  e.getMessage().contains('Script-thrown exception') ? true : false;
            System.assertEquals(message, false);
        }
        //System.assertNotEquals(task.Activity_ID__c != null, null, 'Task Activity ID is not null');
        Test.stopTest();
    }
}
AshwiniAshwini (Salesforce Developers) 
Hi @23,
To achieve code coverage for your TaskBeforeDeleteController class, You can follow below steps:
1.Properly set up related test data in your @TestSetup method.
2.In your test method, directly callTaskBeforeDeleteController.beforeDelete(taskList).
3.Verify that the error message is correctly added to Task records after the method call.
4.Ensure that field-level security settings allow access to required fields.
5.Remove unnecessary Test.startTest() and Test.stopTest() calls as they're not needed for synchronous code testing.

The below articles give a good insight into how coverage can be improved:
 
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 

Code Coverage Best Practices
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_code_coverage_best_pract.htm
 
Checking Code Coverage
https://help.salesforce.com/articleView?id=code_dev_console_tests_coverage.htm&type=5
 
Deployment related code coverage issues
https://help.salesforce.com/articleView?id=000335222&type=1&mode=1
 
https://salesforce.stackexchange.com/questions/148894/help-required-to-get-full-code-covered-in-test-class


If this information helps, please mark the answer as best. Thank you