I'm trying to run a unit test to see if the case owner upon insertion matches an expected value. In this case, the expected case owner will be the account owner. However, upon inserting the case, the case owner changes to a queue. The queue in this case is our "default" queue, which is configured in the support settings module. I recently deactivated all of the triggers on the case object to see if the owner was changing after insert, but that does not seem to be the issue.


    CreateTicket obj = new CreateTicket();
    Case newCase = obj.createcase();
    System.debug('The owner on the new case is a user' + newcase.ownerid);
    Database.dmloptions dmo = new Database.DMLOPtions();
    dmo.assignmentRuleHeader.useDefaultRule = false;
    insert newCase;

String expectedOwner = 'Expected Owner';
String actualOwner = [SELECT Id, Ownerid FROM Case WHERE Id = :newCase.id LIMIT 1][0].owner.name
System.assertEquals(expectedOwner, actualOwner , 'owners do not match');

Any ideas?

  • 3
    Check your assignment rules Commented Mar 30, 2017 at 15:09
  • You should really assert on OnwerId, not that it makes much difference here. Perhaps you have a Workflow Rule in play?
    – Adrian Larson
    Commented Mar 30, 2017 at 15:15
  • 1
    Check your debug logs from running the test. Whatever is going on in should have a hint in the logs.
    – sfdcfox
    Commented Mar 30, 2017 at 15:15
  • It may not be triggering any assignment rule, so is ending up with the default owner
    – gorav
    Commented Mar 30, 2017 at 18:38
  • 1
    Something to with OwnerId, I'd imagine. It might be assignment rules, workflow rules, process builder/flows, or apex code. Just anything that seems to be assigning the value. Try searching for the owner's ID in the debug logs, maybe something will show up.
    – sfdcfox
    Commented Mar 30, 2017 at 21:30

1 Answer 1


So it turns out the test apex execution runs in a different context than the developer console. For this to work through setup, I needed to ensure that the cases are routed through the assignment queues. Previously, the cases would be inserted and the owner would be the user who kicked off the test (me).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .