I have written a test class to cover logic written on before update trigger. The test class is failing at the assert step and upon checking i found that OwnerId in both trigger.new and trigger.oldmap are same even when i am updating it. Below is the code, request you yo kindly help me find the issue.
Method of handler class called from trigger:
public static void wmgRemoveOldGroupQueue(Map<Id,Case> newMap, Map<Id,Case> oldMap){
String wmgRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('WMG_Operations_Workflow').getRecordTypeId();
for (Case o : newMap.values()) {
if (o.RecordTypeId == wmgRecordTypeId && !((String)o.OwnerId).startsWith('005')
&& (o.OwnerId != oldMap.get(o.Id).OwnerId)) {
newMap.get(o.Id).ANN_Accepted_From_Queue__c = '';
}
}
}
Test class:
static testMethod void wmgRemoveOldQueueTest() {
//setup
User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
String wmgRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('WMG_Operations_Workflow').getRecordTypeId();
String assertMsg = 'Accepted From Queue did not clear out';
Group q = new Group();
q.Name = 'WMG Test';
q.Type = 'Queue';
insert q;
QueueSObject qSobj = new QueueSObject();
qSobj.QueueId = q.Id;
qSobj.SobjectType = 'Case';
System.runAs(thisUser){
insert qSobj;
}
Case c = new Case();
c.OwnerId = thisUser.Id;
c.RecordTypeId = wmgRecordTypeId;
c.ANN_Accepted_From_Queue__c = 'WMG Test';
insert c;
c = [SELECT Id, OwnerId FROM Case WHERE Id =: c.Id];
Test.startTest();
c.OwnerId = q.Id;
update c;
Test.stopTest();
c = [SELECT Id, OwnerId, ANN_Accepted_From_Queue__c FROM Case WHERE Id =: c.Id];
System.assertEquals(null, c.ANN_Accepted_From_Queue__c, assertMsg);
} // -- failing at this step
wmgRemoveOldGroupQueue()
is called by your trigger might help. How are you determining that the ownerId intrigger.new
andtrigger.oldMap
are the same?(String)o.OwnerId).startsWith('005')
; useo.OwnerId.getSobjectType() == User.SobjectType
newMap.get(o.Id).ANN_Accepted_From_Queue__c = '';
but you assertSystem.assertEquals(null, c.ANN_Accepted_From_Queue__c, assertMsg);
empty string is not same as null