Salesforce单元测试Assert语句失败,Apex逻辑未触发求助
ANN_Accepted_From_Queue__c First, let's pinpoint the core issue: your debug logs show the Case's OwnerId hasn't actually changed to the Queue ID after your update DML. That means the condition o.OwnerId != oldMap.get(o.Id).OwnerId in wmgRemoveOldGroupQueue never evaluates to true, so the field never gets cleared—hence your assert fails.
Root Causes & Fixes
1. Your Queue isn't configured to handle the WMG Case Record Type
When you create a Queue in Salesforce, you have to explicitly associate it with the record types of the objects it should own. Your test creates a QueueSObject but only links it to the Case object, not the specific WMG_Operations_Workflow record type. Salesforce blocks assigning a record to a Queue that doesn't support its record type, so your update silently fails to change the OwnerId.
Fix the Queue configuration in your test:
System.runAs(thisUser) { QueuesObject q = new QueueSObject( QueueID = testGroup.Id, SObjectType = 'Case', RecordTypeId = wmgRecordTypeId // Add this line to link the Queue to your record type ); insert q; }
2. Verify the Update Actually Changes the Owner
Add an extra assert right after your test query to confirm the OwnerId was updated. This will quickly confirm if the DML is working as expected:
case r = [SELECT id, Ownerid, ANN_Accepted_From_Queue__c FROM Case WHERE Id = :newCase.Id]; // Add this to validate Owner change first System.assertEquals(testGroup.Id, r.OwnerId, 'Case should be owned by the test Queue'); System.assertEquals('', r.ANN_Accepted_From_Queue__c, assertMsg);
3. Permissions Might Be Blocking the Owner Transfer
The user running the test (in your code, thisUser) needs the Transfer Records permission, plus access to the test Queue. If you're not using an admin user, create a test user with full permissions to avoid permission-related blocks:
// Create an admin test user (simplified example) Profile adminProfile = [SELECT Id FROM Profile WHERE Name = 'System Administrator']; User testAdmin = new User( ProfileId = adminProfile.Id, Username = 'testadmin@example.com', Email = 'testadmin@example.com', LastName = 'Admin', FirstName = 'Test', Alias = 'tadmin', TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', EmailEncodingKey = 'UTF-8', LanguageLocaleKey = 'en_US' ); insert testAdmin; // Run the Queue setup and update as this admin System.runAs(testAdmin) { QueuesObject q = new QueueSObject( QueueID = testGroup.Id, SObjectType = 'Case', RecordTypeId = wmgRecordTypeId ); insert q; case newCase = [SELECT id, Ownerid FROM Case WHERE Id = :c.Id]; newCase.OwnerId = testGroup.Id; update newCase; }
4. Add Safeguards in Your Logic Class
To avoid null pointer exceptions (in case a record in newMap doesn't exist in oldMap, though unlikely for updates), modify your loop to check if oldMap contains the Case ID first:
for (Case o : newMap.values()) { if (oldMap.containsKey(o.Id)) { // Add this check Case oldCase = oldMap.get(o.Id); system.debug('o.OwnerId == '+o.OwnerId+' oldCase.OwnerId =='+oldCase.OwnerId); system.debug('o.RecordTypeId '+o.RecordTypeId+ 'wmgRecordTypeId'+wmgRecordTypeId); if (o.RecordTypeId == wmgRecordTypeId && !((String)o.OwnerId).startsWith('005') && (o.OwnerId != oldCase.OwnerId)) { o.ANN_Accepted_From_Queue__c = ''; } system.debug('o.ANN_Accepted_From_Queue__c' +o.ANN_Accepted_From_Queue__c); } }
Final Notes
Start with verifying the OwnerId change first—if that assert fails, the Queue configuration/permissions are definitely your issue. Fixing that will make your original assert pass once the logic triggers as expected.
内容的提问来源于stack exchange,提问作者AB Kumar

