+ Start a Discussion
John NeilanJohn Neilan 

Increase Test Coverage

I've created a trigger and handler class on Tasks that fires when tasks are created, edited, and deleted.  Custom checkboxes are either checked or unchecked on the Opportunity, based upon changes to the subject of the task, the ActivityDate, and the Status.  The trigger works fine.  I now created the test below to test all the potential iterations and I am only geting 63% coverage.  Can anyone help me figure out how to get the bolded lines covered in my handler class?

Handler Class:
public class ClassRenewalTasks {
        public void deleteTasks(List<Task> delTasks) {
            List<Opportunity> linkedOpps = new List<Opportunity>();
            Map<Id, Task> taskMap = new Map<Id, Task>();
            Map<Id, Task> taskMapIds = new Map<Id, Task>();
     
            for (Task t: delTasks) {
                    taskMap.put(t.WhatId, t);
            }
            for (Task t2: delTasks) {
                    taskMapIds.put(t2.Id, t2);
            }
            if (taskMap.size() > 0)
            {
                linkedOpps = [SELECT Id,Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c,
                                    RP_Initial_Program_Developed__c,
                                    RP_Training_Primary_Goal__c,
                                    RP_Review_Initial_Campaign__c,
                                    RP_Business_Review_Complete__c,
                                    RP_Roadmap_Review_Complete__c,
                                    RP_Vision_Meeting__c,
                                    RP_Recommendation_Review_Call__c
                                FROM Opportunity
                                WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE];

                for (Opportunity opp1: linkedOpps){
                    for (Task delTask:[SELECT Id, WhatId, Subject, Status, ActivityDate, IsDeleted
                                        FROM Task
                                        WHERE Id IN: taskMapIds.keySet() AND WhatId =: opp1.Account.Id AND Status =: 'Completed' AND ActivityDate >=: opp1.Effective_Date__c AND ActivityDate <=: opp1.Renewal_Date_Next__c]){
system.debug('******** delTask ' + delTask);

                        IF(delTask.Subject == 'User Training Complete'){
                            opp1.RP_User_Training__c = FALSE;

                        }
                        ELSE IF(delTask.Subject == 'Initial Program Confirmation'){
                            opp1.RP_Initial_Program_Developed__c = FALSE;

                        }
                        ELSE IF(delTask.Subject == 'Primary Goal Training Confirmation'){
                            opp1.RP_Training_Primary_Goal__c = FALSE;

                        }
                        ELSE IF(delTask.Subject == 'Initial Campaign Results'){
                            opp1.RP_Review_Initial_Campaign__c = FALSE;

                        }
                        ELSE IF(delTask.Subject == 'Business Review'){
                            opp1.RP_Business_Review_Complete__c = FALSE;

                        }
                        ELSE IF(delTask.Subject == 'Roadmap Review'){
                            opp1.RP_Roadmap_Review_Complete__c = FALSE;

                        }
                        ELSE IF(delTask.Subject == 'Manager Introduction'){
                            opp1.RP_Vision_Meeting__c = FALSE;

                        }
                        ELSE IF(delTask.Subject == 'Renewal Package Review'){
                            opp1.RP_Recommendation_Review_Call__c = FALSE;

                        }
                    }
                }
                if (linkedOpps.size() > 0)
                {
                update linkedOpps;
                }
            }
        }

        public void renewalTasks(List<Task> checkTasks) {
            List<Opportunity> linkedOpps = new List<Opportunity>();
            Map<Id, Task> taskMap = new Map<Id, Task>();

            for (Task t: checkTasks) {
                if (t.WhatId  != null && (t.Subject == 'User Training Complete' ||
                                        t.Subject == 'Initial Program Confirmation' ||
                                        t.Subject == 'Primary Goal Training Confirmation' ||
                                        t.Subject == 'Initial Campaign Results' ||
                                        t.Subject == 'Business Review' ||
                                        t.Subject == 'Roadmap Review' ||
                                        t.Subject == 'Manager Introduction' ||
                                        t.Subject == 'Renewal Package Review')){
                    taskMap.put(t.WhatId, t);
    System.Debug('******** TaskMapEdit ' + taskMap);
                }
            }

            if (taskMap.size() > 0)
            {
                linkedOpps = [SELECT Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c,
                                    RP_Initial_Program_Developed__c,
                                    RP_Training_Primary_Goal__c,
                                    RP_Review_Initial_Campaign__c,
                                    RP_Business_Review_Complete__c,
                                    RP_Roadmap_Review_Complete__c,
                                    RP_Vision_Meeting__c,
                                    RP_Recommendation_Review_Call__c
                                FROM Opportunity 
                                WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE];

                for (Opportunity c: linkedOpps){
                    for (Task taskAdd:[SELECT WhatId, Subject, Status, ActivityDate
                                        FROM Task
                                        WHERE WhatId =: c.Account.Id AND Status =: 'Completed' AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Renewal_Date_Next__c]){
                        IF(taskAdd.Subject == 'User Training Complete' && taskAdd.Status == 'Completed'){
                            c.RP_User_Training__c = TRUE;
                        }
                        IF(taskAdd.Subject == 'Initial Program Confirmation'){
                            c.RP_Initial_Program_Developed__c = TRUE;
                        }
                        IF(taskAdd.Subject == 'Primary Goal Training Confirmation'){
                            c.RP_Training_Primary_Goal__c = TRUE;
                        }
                        IF(taskAdd.Subject == 'Initial Campaign Results'){
                            c.RP_Review_Initial_Campaign__c = TRUE;
                        }
                        IF(taskAdd.Subject == 'Business Review'){
                            c.RP_Business_Review_Complete__c = TRUE;
                        }
                        IF(taskAdd.Subject == 'Roadmap Review'){
                            c.RP_Roadmap_Review_Complete__c = TRUE;
                        }
                        IF(taskAdd.Subject == 'Manager Introduction'){
                            c.RP_Vision_Meeting__c = TRUE;
                        }
                        IF(taskAdd.Subject == 'Renewal Package Review'){
                            c.RP_Recommendation_Review_Call__c = TRUE;
                        }
                    }
                    for (Task taskRem:[SELECT WhatId, Subject, Status, ActivityDate
                                        FROM Task
                                        WHERE WhatId =: c.Account.Id AND ((Status !=: 'Completed' AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Renewal_Date_Next__c) OR
                                        (Status =: 'Completed' AND (ActivityDate <: c.Effective_Date__c OR ActivityDate >: c.Renewal_Date_Next__c)) OR
                                        (Status !=: 'Completed' AND (ActivityDate <: c.Effective_Date__c OR ActivityDate >: c.Renewal_Date_Next__c)))]){
                        IF(taskRem.Subject == 'User Training Complete'){
                            c.RP_User_Training__c = FALSE;
                        }
                        IF(taskRem.Subject == 'Initial Program Confirmation'){
                            c.RP_Initial_Program_Developed__c = FALSE;
                        }
                        IF(taskRem.Subject == 'Primary Goal Training Confirmation'){
                            c.RP_Training_Primary_Goal__c = FALSE;
                        }
                        IF(taskRem.Subject == 'Initial Campaign Results'){
                            c.RP_Review_Initial_Campaign__c = FALSE;
                        }
                        IF(taskRem.Subject == 'Business Review'){
                            c.RP_Business_Review_Complete__c = FALSE;
                        }
                        IF(taskRem.Subject == 'Roadmap Review'){
                            c.RP_Roadmap_Review_Complete__c = FALSE;
                        }
                        IF(taskRem.Subject == 'Manager Introduction'){
                            c.RP_Vision_Meeting__c = FALSE;
                        }
                        IF(taskRem.Subject == 'Renewal Package Review'){
                            c.RP_Recommendation_Review_Call__c = FALSE;
                        }
                    }
                }

                // if the list of cons isnt empty, update them
                system.debug('linkedOpps = '+linkedOpps);
                if (linkedOpps.size() > 0)
                {
    System.Debug('******** linkedOpps Edit ' + linkedOpps);
                update linkedOpps;
                }
            }
        }
    }

Test Class:
@isTest
public class TestRenewalTasks{

    static testmethod void testRenewalTasks(){
        
        Profile ProSys = [SELECT Id
                          FROM Profile
                          WHERE Name='Client Services (Admin)'];
        User U1 = new User(Alias = 'User1',Country='United States',Email='User1@testing.com',EmailEncodingKey='ISO-8859-1', LastName='User1', 
                            LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = ProSys.Id,TimeZoneSidKey='America/New_York', UserName='User1@testing.com');
        insert U1;


        Account acct1 = new Account();
        acct1.Name = 'Test-JN';
        acct1.Region__c = 'USA';
        acct1.BillingCountry = 'USA';
        acct1.Industry = 'Consumer Goods';
        acct1.Status__c = 'Customer';
        acct1.Website = 'www.test.com';
        acct1.FB_Page_1_Fans__c = 500;
        acct1.FB_Page_1_Link__c = 'www.facebook.com/test';
        acct1.OwnerId = U1.Id;
        INSERT acct1;

        Opportunity opp1 = new Opportunity();
        opp1.Name = 'Test Opp';
        opp1.StageName = 'Phase 1';
        opp1.CloseDate = date.parse('1/1/15');
        opp1.Renewal__c = 'Yes';
        opp1.Effective_Date__c = date.parse('1/1/15');
        opp1.Term__c = 12;
        opp1.AccountId = acct1.Id;
        INSERT opp1;

        
        Task tsk1 = new task();
        tsk1.WhatId = acct1.Id;
        tsk1.ActivityDate = date.parse('1/5/15');
        tsk1.Subject = 'Initial Program Confirmation';
        tsk1.Status = 'Completed';
        tsk1.Priority = 'Normal';
        INSERT tsk1;

            Task tsk1a = [SELECT Id,ActivityDate,Status
                        FROM Task
                        WHERE Id =: tsk1.Id];
                tsk1a.Status = 'In Progress';
            UPDATE tsk1a;
                tsk1a.Status = 'Completed';
                tsk1a.ActivityDate = date.parse('1/5/14');
            UPDATE tsk1a;
                tsk1a.Status = 'In Progress';
            UPDATE tsk1a;

        Task tsk2 = new task();
        tsk2.WhatId = acct1.Id;
        tsk2.ActivityDate = date.parse('1/5/15');
        tsk2.Subject = 'User Training Complete';
        tsk2.Status = 'Completed';
        tsk2.Priority = 'Normal';
        INSERT tsk2;

            Task tsk2a = [SELECT Id,ActivityDate,Status
                        FROM Task
                        WHERE Id =: tsk2.Id];
                tsk2a.Status = 'In Progress';
            UPDATE tsk2a;
                tsk2a.Status = 'Completed';
                tsk2a.ActivityDate = date.parse('1/5/14');
            UPDATE tsk2a;
                tsk2a.Status = 'In Progress';
            UPDATE tsk2a;

        
        Task tsk3 = new task();
        tsk3.WhatId = acct1.Id;
        tsk3.ActivityDate = date.parse('1/5/15');
        tsk3.Subject = 'Primary Goal Training Confirmation';
        tsk3.Status = 'Completed';
        tsk3.Priority = 'Normal';
        INSERT tsk3;

            Task tsk3a = [SELECT Id,ActivityDate,Status
                        FROM Task
                        WHERE Id =: tsk3.Id];
                tsk3a.Status = 'In Progress';
            UPDATE tsk3a;
                tsk3a.Status = 'Completed';
                tsk3a.ActivityDate = date.parse('1/5/14');
            UPDATE tsk3a;
                tsk3a.Status = 'In Progress';
            UPDATE tsk3a;
        
        Task tsk4 = new task();
        tsk4.WhatId = acct1.Id;
        tsk4.ActivityDate = date.parse('1/5/15');
        tsk4.Subject = 'Initial Campaign Results';
        tsk4.Status = 'Completed';
        tsk4.Priority = 'Normal';
        INSERT tsk4;

            Task tsk4a = [SELECT Id,ActivityDate,Status
                        FROM Task
                        WHERE Id =: tsk4.Id];
                tsk4a.Status = 'In Progress';
            UPDATE tsk4a;
                tsk4a.Status = 'Completed';
                tsk4a.ActivityDate = date.parse('1/5/14');
            UPDATE tsk4a;
                tsk4a.Status = 'In Progress';
            UPDATE tsk4a;
        
        Task tsk5 = new task();
        tsk5.WhatId = acct1.Id;
        tsk5.ActivityDate = date.parse('1/5/15');
        tsk5.Subject = 'Business Review';
        tsk5.Status = 'Completed';
        tsk5.Priority = 'Normal';
        INSERT tsk5;

            Task tsk5a = [SELECT Id,ActivityDate,Status
                        FROM Task
                        WHERE Id =: tsk5.Id];
                tsk5a.Status = 'In Progress';
            UPDATE tsk5a;
                tsk5a.Status = 'Completed';
                tsk5a.ActivityDate = date.parse('1/5/14');
            UPDATE tsk5a;
                tsk5a.Status = 'In Progress';
            UPDATE tsk5a;
        
        Task tsk6 = new task();
        tsk6.WhatId = acct1.Id;
        tsk6.ActivityDate = date.parse('1/5/15');
        tsk6.Subject = 'Roadmap Review';
        tsk6.Status = 'Completed';
        tsk6.Priority = 'Normal';
        INSERT tsk6;

            Task tsk6a = [SELECT Id,ActivityDate,Status
                        FROM Task
                        WHERE Id =: tsk6.Id];
                tsk6a.Status = 'In Progress';
            UPDATE tsk6a;
                tsk6a.Status = 'Completed';
                tsk6a.ActivityDate = date.parse('1/5/14');
            UPDATE tsk6a;
                tsk6a.Status = 'In Progress';
            UPDATE tsk6a;
        
        Task tsk7 = new task();
        tsk7.WhatId = acct1.Id;
        tsk7.ActivityDate = date.parse('1/5/15');
        tsk7.Subject = 'Manager Introduction';
        tsk7.Status = 'Completed';
        tsk7.Priority = 'Normal';
        INSERT tsk7;

            Task tsk7a = [SELECT Id,ActivityDate,Status
                        FROM Task
                        WHERE Id =: tsk7.Id];
                tsk7a.Status = 'In Progress';
            UPDATE tsk7a;
                tsk7a.Status = 'Completed';
                tsk7a.ActivityDate = date.parse('1/5/14');
            UPDATE tsk7a;
                tsk7a.Status = 'In Progress';
            UPDATE tsk7a;
        
        Task tsk8 = new task();
        tsk8.WhatId = acct1.Id;
        tsk8.ActivityDate = date.parse('1/5/15');
        tsk8.Subject = 'Renewal Package Review';
        tsk8.Status = 'Completed';
        tsk8.Priority = 'Normal';
        INSERT tsk8;

            Task tsk8a = [SELECT Id,ActivityDate,Status
                        FROM Task
                        WHERE Id =: tsk8.Id];
                tsk8a.Status = 'In Progress';
            UPDATE tsk8a;
                tsk8a.Status = 'Completed';
                tsk8a.ActivityDate = date.parse('1/5/14');
            UPDATE tsk8a;
                tsk8a.Status = 'In Progress';
            UPDATE tsk8a;
        
        Task tsk9 = new task();
        tsk9.WhatId = acct1.Id;
        tsk9.ActivityDate = date.parse('1/5/15');
        tsk9.Subject = 'Test Task 1';
        tsk9.Status = 'Completed';
        tsk9.Priority = 'Normal';
        INSERT tsk9;

            Task tsk9a = [SELECT Id,ActivityDate,Status
                        FROM Task
                        WHERE Id =: tsk9.Id];
                tsk9a.Status = 'In Progress';
            UPDATE tsk9a;
                tsk9a.Status = 'Completed';
                tsk9a.ActivityDate = date.parse('1/5/14');
            UPDATE tsk9a;
                tsk9a.Status = 'In Progress';
            UPDATE tsk9a;

    Test.startTest();
        DELETE tsk1;
        DELETE tsk2;
        DELETE tsk3;
        DELETE tsk4;
        DELETE tsk5;
        DELETE tsk6;
        DELETE tsk7;
        DELETE tsk8;
        DELETE tsk9;
    Test.stopTest();
    }
}

 
pconpcon
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

In your case, I would severly reduce the size of your test case. There is no reason to inserting then updating these tasks, when they could be just inserted with the updated data.  To reach those lines of code, you will need to setup your data  so that you retreive tasks back that meet those criteria in your SOQL query and your if statements. It is important to remember that deleted data will not show up in your SOQL results.  I would recommend that you try to write a single test case for each if block.  Then once you have a single test for each one of those write a test case that hits all of them at once.

Each test should follow the following structure:
  • Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  • Starting the test. This is calling Test.startTest() to reset the governor limits
  • Calling your class / method
  • Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  • Asserting that your changes have worked
    • If you have inserted/updated/deleted data, you need to query for the updates
    • Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/