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
Michael Kolodner 13Michael Kolodner 13 

What is wrong with my test?

I'm working on sandbox refresh code. For some reason, the portion of my test that makes sure I've inserted the task I want it not getting any code coverage. Here's my class:
global class SandboxRefreshScript implements SandboxPostCopy {
    global void runApexClass(SandboxContext context) {
        // Change value of Alias below to the Alias of user who should own the group
        // ensuring that it is unique among your users
        List<User> groupOwner = [SELECT Id, Username FROM User WHERE Alias = 'MKolo'];
        Id grpOwnerId = groupOwner[0].Id;
        
        // Create a Chatter Group for Payment Pipeline Changes
        List<CollaborationGroup> groups = new List<CollaborationGroup>();
        
        CollaborationGroup pipeline = new CollaborationGroup(
            CollaborationType = 'Public',
            Description = 'Group for showing changes to the cashflow pipeline. ',
            Name = 'Payment Pipeline Changes',
            OwnerId = grpOwnerId
        );
        groups.add(pipeline);
        
        //Create another Chatter Group for All Spark
        CollaborationGroup allSpark = new CollaborationGroup(
            CollaborationType = 'Public',
            Description = 'All Spark group. ',
            Name = 'All Spark',
            OwnerId = grpOwnerId
        );
        groups.add(allSpark);
        
        //Create another Chatter Group for Hiring Notifications
        CollaborationGroup hiringNotifications = new CollaborationGroup(
            CollaborationType = 'Private',
            Description = 'Hiring Notifications group. ',
            Name = 'Hiring Notifications',
            OwnerId = grpOwnerId
        );
        groups.add(hiringNotifications);
        
        insert groups;
        
        //Create a Task for yesterday (so it's immediately overdue) that
        //reminds to update Process Builders to use the new Chatter Groups.
        Task newTask = new Task(
        	Subject = 'Fix Process Builders',
            ActivityDate = Date.today()-1
        );
        insert newTask;

    }
}

And here's my test class:
@isTest
class TestSandboxRefreshScript {
    
    @isTest
    static void testMySandboxPrep() {
        
        Test.startTest();
        
        Test.testSandboxPostCopyScript(
            new SandboxRefreshScript(), UserInfo.getOrganizationId(),
            UserInfo.getOrganizationId(), UserInfo.getOrganizationName());
        
        Test.stopTest();
        
        // Test for Payment Pipeline Changes
        List<CollaborationGroup> chGroup = [SELECT Id, CollaborationType from CollaborationGroup WHERE Name = 'Payment Pipeline Changes'];
        System.assertEquals(1, chGroup.size(), 'Chatter Group List size not equal to 1');
        System.assertEquals('Public', chGroup[0].CollaborationType, 'Chatter group is not public');
        
        // Test for All Spark
        List<CollaborationGroup> chGroup2 = [SELECT Id, CollaborationType from CollaborationGroup WHERE Name = 'All Spark'];
        System.assertEquals(1, chGroup.size(), 'Chatter Group List size not equal to 1');
        System.assertEquals('Public', chGroup[0].CollaborationType, 'Chatter group is not public');
        
        // Test for Hiring Notifications
        List<CollaborationGroup> chGroup3 = [SELECT Id, CollaborationType from CollaborationGroup WHERE Name = 'Hiring Notifications'];
        System.assertEquals(1, chGroup.size(), 'Chatter Group List size not equal to 1');
        System.assertEquals('Private', chGroup[0].CollaborationType, 'Chatter group is not private');
        
        //Test for Task Insert
        List<Task> tasks = [SELECT Id from Task WHERE ActivityDate = Yesterday];
        System.assertEquals(1, tasks.size(), 'Task Group List size not equal to 1');

    }
}

When I run the test,  there is no coverage for lines 41-45 and I don't understand why?
Best Answer chosen by Michael Kolodner 13
thomastthomast
That's very odd.  I'd say just chalk it up to weirdness and deploy anyway since you're over 75% and the task is clearly getting inserted.

All Answers

Michael Kolodner 13Michael Kolodner 13
Nope. That won't even compile.
Michael Kolodner 13Michael Kolodner 13
You've completely lost me. I am pretty sure the problem can't be the way I've added days for two reasons:
1. My way compiles. But if I try to replace with Date.today().addDays(-1), it won't even compile.
2. If I copy lines 39-45 and run them in Execute Anonymous I end up with one task and then am able to use the SOQL query in the query editor and it comes back with one result.
 
Michael Kolodner 13Michael Kolodner 13
I just created a standalone class:
public class TryingTask {
    
    public static void insertTask() {
        //Create a Task for yesterday (so it's immediately overdue) that
        //reminds to update Process Builders to use the new Chatter Groups.
        Task newTask = new Task(
            Subject = 'Fix Process Builders',
            ActivityDate = Date.today()-1
        );
        insert newTask;
    }
}
and a test class:
@isTest
public class TestTryingTask {

    @isTest
    public static void testTryTask(){
        TryingTask.insertTask();
        //Test for Task Insert
        List<Task> tasks = [SELECT Id from Task WHERE ActivityDate = Yesterday];
        System.debug('test about to run');
        System.assertEquals(1, tasks.size(), 'Task Group List size not equal to 1');
    }
}

That test runs fine, passes with 100% code coverage.

I guess I'm missing something about the way the SandboxRefreshScript is written? I saw that there is no specific method for creating the ChatterGroups, so I did exactly the same with creating a Task, but that part didn't get coverage. 
 
thomastthomast
Is the test succeeding but not covering those lines, or is the test failing? It sounds like it's succeeding but not giving coverage, but I just want to make sure.

Here's one theory, untested - the context user for the sandbox refresh script is not one that can own tasks, and so your task insertion is failing. I'm surprised you're not getting an error to that effect, though. Try setting OwnerId to the same owner you're setting for the CollaborationGroups and see if that helps. 

 
Michael Kolodner 13Michael Kolodner 13
Indeed. Test succeeds but does not cover those lines.
Michael Kolodner 13Michael Kolodner 13
I tried putting in an OwnerID, as you said. Test still runs successfully just without covering the task insertion lines at all.
thomastthomast
The per-line coverage indicators in Dev Console can get a little out-of-whack. Is it showing less than 100% coverage in the "Overall Code Coverage" box to the right, when you select your TestRun results in the Tests tab? Also, try closing the main class file in DevConsole, and then re-opening by double-clicking its line in that Overall Code Coverage box. And/or using the "Code Coverage: All Tests nn%" button at the top to change to showing coverage from your specific test class, or None, and then back again. That can sometimes refresh the line indicators properly. But I can't see how your test could pass and not cover those lines.
thomastthomast
I copy-pasted your two classes and got 100% coverage running the test. You may well have found and fixed this by now, but the list variable names in your 2nd & 3rd sets of asserts for the CollaborationGroups need to be updated to match their corresponding queries. 
Michael Kolodner 13Michael Kolodner 13
:-( Thanks for the catch on the variable names. I've fixed that. But I still get only 82% coverage after trying what you indicated. 
User-added image
thomastthomast
That's very odd.  I'd say just chalk it up to weirdness and deploy anyway since you're over 75% and the task is clearly getting inserted.
This was selected as the best answer
Michael Kolodner 13Michael Kolodner 13
Dammit! Weirdness it is. I just copied into a different sandbox and get 100% coverage. I hate to be marking the "deploy anyway" answer as Best, but it kinda' is...