I've a batch class which identifies the users which are not added to list of chatter groups (CollaborationGroups) and adds them to the groups by creating collaborationGroupMember records.

In order to test this batch apex class, I've written a test class:

  • Created three user records
  • Created two collaborationgroups
  • Schedule batch apex within Test.startTest() and Test.stopTest()
  • Now when I'm doing assertion in the test class, it fails --> Using debug statements before and after (and also within) Test.startTest() and Test.stopTest() it seems that collaborationGroupMembers are not created at all i.e. the members are not added to the group

But from the debug statements of the batch apex, I could see that the collaborationGroupMembers were successfully inserted for those three user records without any error.

Am I missing something here, or is this behaviour due to test context?

Thanks in advance!

  • Is the test class for batch or scheduler? What are you asserting?
    – manjit5190
    Apr 29 '20 at 14:03
  • Can you please provide code from your batch class and test? Apr 29 '20 at 14:04
  • @manjit5190 The test class is for scheduler which ultimately executes the batch class and covers code of Batch class as well.
    – sfdcnewbie
    Apr 29 '20 at 14:16
  • Can you have a look at this. salesforce.stackexchange.com/questions/93670/…
    – manjit5190
    Apr 29 '20 at 14:17
  • @manjit5190 Thanks - this worked! The assertions are not failing. I had to explicitly call the execute() of schedulable to make it work. As per the link, this was because the batch was never executed by schedulable when scheduling from test class. But in my case, I could see the debug statements from batch class. This means the batch class was executed. But the result of batch was not 'collected'. When I called the execute() of schedulable separately, everything went fine. Any reason for this. Thanks a lot!!
    – sfdcnewbie
    Apr 29 '20 at 14:41

Your Answer

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

Browse other questions tagged or ask your own question.