You need to sign in to do that
Don't have an account?
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:
And here's my test class:
When I run the test, there is no coverage for lines 41-45 and I don't understand why?
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?
All Answers
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.
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.
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.