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
Elliot32Elliot32 

Help improving Code Coverage for Task Trigger

Hi All,

Please see below Task Trigger (1st) and Test Class (2nd):
 
Trigger EPS_LeadStatusOppStageToTask on Task (before insert, before update) {
    
    String lead_prefix = Schema.SObjectType.Lead.getKeyPrefix();
    String opportunity_prefix = Schema.SObjectType.Opportunity.getKeyPrefix();
    
    Map<Id, List<Task>> whoWhatIds = new Map<Id, List<Task>>{};

    For (Task t : trigger.new) {
        
        If (Trigger.isUpdate) {
        
        Task oldTask=Trigger.oldMap.get(t.Id);
    
        Boolean oldTaskIsCompleted=oldTask.Status.equals('Completed');
        Boolean newTaskIsCompleted=t.Status.equals('Completed');
        
        If (t.WhatId == null && t.WhoId != null && ((String)t.WhoId).startsWith(lead_prefix) && (!oldTaskIsCompleted && newTaskIsCompleted)) {
            List<Task> tasks = whoWhatIds.get(t.WhoId);
            If (tasks == null) {
                tasks = new List<Task>{};
                whoWhatIds.put(t.WhoId, tasks);
                tasks.add(t);
            }
        } Else If (t.WhatId != null && ((String)t.WhatId).startsWith(opportunity_prefix) && (!oldTaskIsCompleted && newTaskIsCompleted)) {
            List<Task> tasks1 = whoWhatIds.get(t.WhatId);
            If (tasks1 == null) {
                tasks1 = new List<Task>{};
                whoWhatIds.put(t.WhatId, tasks1);
                tasks1.add(t);
            }
        }
        }
        
        If(Trigger.isInsert) {
        
        If (t.WhatId == null && t.WhoId != null && ((String)t.WhoId).startsWith(lead_prefix) && t.Status == 'Completed') {
            List<Task> tasks = whoWhatIds.get(t.WhoId);
            If (tasks == null) {
                tasks = new List<Task>{};
                whoWhatIds.put(t.WhoId, tasks);
                tasks.add(t);
            }
        } Else If (t.WhatId != null && ((String)t.WhatId).startsWith(opportunity_prefix) && t.Status == 'Completed') {
            List<Task> tasks1 = whoWhatIds.get(t.WhatId);
            If (tasks1 == null) {
                tasks1 = new List<Task>{};
                whoWhatIds.put(t.WhatId, tasks1);
                tasks1.add(t);
            }
        }
        }
    }

    For (Lead ld : [Select Id, Name, Status from lead where Id in :whoWhatIds.keySet()]) {
        For(Task t2 : whoWhatIds.get(ld.id)) {
            t2.Lead_Opp_Status_Stage__c = ld.Status;
        }
    }

    For(Opportunity opp : [Select Id, Name, StageName from Opportunity where Id in :whoWhatIds.keySet()]) {
        For(Task t3 : whoWhatIds.get(opp.id)) {
            t3.Lead_Opp_Status_Stage__c = opp.StageName;
        }
    }
}
 
@isTest
Public class EPS_Test_LeadStatusOppStageToTask {

    private static testMethod void myUnitTest() {
        
        Lead newLead = new Lead (
                       LastName = 'Test',
                       Company = 'Test Company',
                       Phone = '3759275838',
                       Industry = 'Arts',
                       Status = 'New'
                       );
        insert newLead;
        
        Account newAcct = new Account (
                          Name = 'Test Company'
                          );
        insert newAcct;
        
        Opportunity newOpp = new Opportunity (
                             Name = 'Test Company',
                             AccountId = newAcct.Id,
                             CloseDate = System.today(),
                             StageName = 'Demo Held'
                             );
        insert newOpp;
                             
        Task newTask = new Task (
                       WhatId = newOpp.Id,
                       Type = 'Call',
                       Status = 'Completed',
                       ActivityDate = date.today()
                       );
                       
        Task newTask1 = new Task (
                        WhoId = newLead.Id,
                        Type = 'Call',
                        Status = 'Completed',
                        ActivityDate = date.today()
                        );
                        
        Task newTask2 = new Task (
                        WhatId = newOpp.Id,
                        Type = 'Call',
                        Status = 'Not Started',
                        ActivityDate = date.today().addDays(5)
                        );
                       
        Task newTask3 = new Task (
                        WhoId = newLead.Id,
                        Type = 'Call',
                        Status = 'Not Started',
                        ActivityDate = date.today().addDays(5)
                        );
                       
        Test.startTest();
        
            insert newTask;
            System.assert(newTask.Lead_Opp_Status_Stage__c == 'New');
            
            insert newTask1;
            System.assert(newTask1.Lead_Opp_Status_Stage__c == 'Demo Held');
            
            insert newTask2;
            newTask2.Status = 'Completed';
            System.assert(newTask2.Lead_Opp_Status_Stage__c == 'New');
            
            insert newTask3;
            newTask3.Status = 'Completed';
            System.assert(newTask3.Lead_Opp_Status_Stage__c == 'Demo Held');
            
        Test.stopTest();
    }
}

The trigger works well enough for my purposes, but I cannot seem to get the code coverage above 56%. Also, when I run the Test, my Method does not Pass!

My question is this: Are there any resources available to help one identify where the test class is lacking in coverage on a particular trigger? ie Where in my trigger is the code not covered?

In addition, are there any particular recommendations for me here to improve coverage? I am a novice Apex developer so this is still a learning experience for me. If you would like to take the time on recommendations for me to improve my code as well, that would be appreciated; the trigger does in fact work, but I am sure there are more succinct ways to accomplish the same thing.

Thanks!
Elliot
pconpcon
You need to focus on the other parts of your tests.  And you need to look at splitting your sobject testing.  Take a read over this [1] to help you develop a testing strategy.

[1] http://blog.deadlypenguin.com/blog/testing/strategies/