1
Following is code of my Test Class 

- Where Asserts failed while deploying in Code into the Production.

I can change System.assert() to anything to avoid this.

But I want to know what I am doing wrong here -

Test class Code :

******* Created few users here user1, user2, user3,user4,etc.

Account testAcc = new Account();
testAcc.Name = 'Acc test DR';         
testAcc.Phone = '5678797808';         
testAcc.Channel_Description__c='asd';
testAcc.Channel_Code__c='qwe';
testAcc.Account_Status__c ='Active';
testAcc.recordTypeid = rtIdIRAccount;
testAcc.OwnerId = user1.Id; // **** notice this here 
testAcc.MC2__c = true;
insert testAcc;

Case cs2 = new Case();
cs2.AccountId = testAcc.Id;  //***** notice this here 
cs2.origin = 'Phone';
cs2.Case_Source__c = 'Customer';
cs2.Status = IRConstants.CASE_NEW;
cs2.IsEscalated = false;
cs2.recordTypeId = npsClosedLoopRTID;
insert cs2;

system.assertEquals(user3.Id, [select ownerId from case where id = : cs2.Id].ownerId);

ERROR :

System.AssertException: Assertion Failed: Expected: 00519000000oBaXAAU, Actual: 00519000000oBaWAAU

Stack Trace: Class.MyTest.updateOwnerToAccountTeamMemeber_Test: line 543, column 1

4
  • 1
    Might be missing something here but you're not setting the Owner of the Case to an Id of a user you created?
    – Dan Jones
    Commented Oct 16, 2015 at 10:02
  • I have process builder for that. and its doing its work just fine Commented Oct 16, 2015 at 10:08
  • So it's automatically assigning the users to the case? Would make sense given you're returning an Id anyway. So, I'd look at who it's assigning it to and the criteria you've specified to assign users to cases. Why should this particular user be assigned to this particular case within your process builder.
    – Dan Jones
    Commented Oct 16, 2015 at 10:11
  • It is very tricky writing robust asserts when Process Builder is involved in the transaction and Users are in play. In testmethods, you can mock new Users but all Users are available to the test (and processes) so one can run into order dependencies when queries are issued between sandbox and PROD. Sometimes in situations above, I change the last assert to a not equals user1.id so I know that some different owner was assigned to the user that I mocked
    – cropredy
    Commented Oct 16, 2015 at 16:04

1 Answer 1

3

I could be missing something here, but in this assert:

system.assertEquals(user3.Id, [select ownerId from case where id = : cs2.Id].ownerId);

You're comparing the Id of a user you've dynamically created to the Owner's Id of a case you've you've created. Specifically you're expecting "user3" to be assigned to "cs2".

However, in the creation of "cs2", you're not actually setting the Owner of a case:

Case cs2 = new Case();
cs2.AccountId = testAcc.Id;  //***** notice this here 
cs2.origin = 'Phone';
cs2.Case_Source__c = 'Customer';
cs2.Status = IRConstants.CASE_NEW;
cs2.IsEscalated = false;
cs2.recordTypeId = npsClosedLoopRTID;
insert cs2;

Your assert though is still retrieving a value though. As you've mentioned in a comment:

I have process builder for that. and its doing its work just fine

So, there's got to be something within that process builder that's assigning a different user to the case than you're expecting.

Therefore, you'll want to look at whatever criteria you've set within that process builder that's determining who gets to own a certain case. Your problem will be in there.

Why should "user3" get assigned to the case and not the user with the Id "00519000000oBaWAAU" given you've automated the ownership process?

Note: I'm not sure why you've included the Account you've dynamically created. Do cases get owned by the user who owns the Account? In which case you've set that to "user1". But there doesn't appear to be enough information here for us to help you, but hopefully this can help narrow down the solution to the cause of the problem.

The good news is that there's nothing wrong with your code here that I can see anyway.

You must log in to answer this question.

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