new developer and I am struggling with getting 100% code coverage on a test class. I have written a class to count tasks with unique subline lines and then update a field on a custom object with the count. Basically, all code below the For(task t : taskList) loop is not running. The method is being called from a trigger handler and the test method is inserting relationship_owners and contacts from a data factory.
Here is the method
public static void uniqueRelationshipOwnerEmails(List<relationship_owner__c> roList){
//count the unique emails that have been sent by a relationship owner
// add all contacts associated with relationship owner to map.
Map<id, Contact> contactMap = new Map<Id, Contact>([SELECT Id,
New_Relationship_Owner1__c
FROM Contact
WHERE New_Relationship_owner1__c IN : roList]);
system.debug('Contacts found = ' + contactMap.size());
//put tasks where whoId is in the contact map into a new map
List<Task> taskList = [SELECT Id, WhoId,Subject
FROM Task
WHERE WhoId IN :contactMap.keySet()
AND Subject LIKE '%Pardot List Email%'];
system.debug('Tasks Found and added to map = ' + taskList.size());
//use set to dedupe the list
Map<Id, Set<String>> subjectLineMap = new Map<Id, Set<String>>();
for(task t : taskList){
Id ownerId = contactMap.get(t.WhoId).New_Relationship_Owner1__c;
if(!subjectLineMap.containsKey(ownerId)){
subjectLineMap.put(ownerId, new Set<String>());
}
}
system.debug('Map size =' + subjectLineMap.size());
system.debug('map values =' + subjectLineMap.values());
system.debug('map keys =' + subjectLineMap.Keyset());
for(relationship_owner__c r : roList){
r.Unique_Emails_Sent__c = subjectLineMap.get(r.Id).size();
roList.add(r);
}
insert(roList);
}
and here is the test method.
@isTest
static void uniqueEmailTest(){
TestDataFactory.createRelationshipOwnerandInfuencer(5, 1);
List<relationship_owner__C> ownerTestList = [SELECT id, Unique_Emails_Sent__c From relationship_owner__c];
//update ownerTestList;
//get the contacts and assign them tasks
List<Contact> influencers = [SELECT Id FROM Contact];
List<Task> tasks = new List<Task>();
for(Contact c : influencers){
Task t = new Task(Subject = 'Pardot List Email 1', WhoId = c.Id);
tasks.add(t);
}
insert tasks;
system.debug('task size' + tasks.size());
update ownerTestList;
}
Thank you for your help.