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,
                                                        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;            
            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();



and here is the test method.

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);
    insert tasks;        
    system.debug('task size' + tasks.size());
    update ownerTestList;

Thank you for your help.

  • You don't really need 100% coverage, honestly I have rarely seen classes covered 100% when building a large scale application. I do try to get more than 90% to accommodate any low coverage classes but most of the times anything around 80 - 85 should be considered good. If you have coverage sufficiently above 75%, then you should be fine. What is your current coverage as of now? Btw - I don't see your test class calling the method which you are trying to cover. – Jayant Das Oct 17 '18 at 21:30
  • one way to help think about testmethods is the Given-When-Then model which forces you to think through in comments what you are trying to test and then you can code the testmethod accordingly. – cropredy Oct 18 '18 at 1:35

You will need to insert Contact records (and relationship_owner__C records) in your test before this line for this query to return any results and so create the Task records needed to cause the code under test to run:

List<Contact> influencers = [SELECT Id FROM Contact];

By design, unit tests don't see most types of records that are in the org: you have to explicitly add records in your test.

(I presume uniqueRelationshipOwnerEmails is called from a trigger?)

The point of tests is to confirm the behavior of the code under test: coverage is just an indicator, not a sufficient condition. So do add additional logic to your test (typically queries and asserts) that confirm the expected effects of the code under test.

  • I am sorry I was not clear in my original post. The method I called from the testDataFactory created 5 relationship owners and 1 contact per owner. I will edit my original post. And the method is called from a trigger handler. Right now I have 66% code coverage. – Brooks Johnson Oct 17 '18 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.