I'm re-writing unit tests for the Apex Specialist superbadge (yes, re-writing, I successfully completed the superbadge a couple of days ago, and found a few bugs while playing around today, decided to try and fix them), all was fine and dandy, tests were passing, asserts were correct, until I started fixing bugs, which introduced more bugs.
First I was updating a record with the update function, then that started throwing the aforementioned error. Changed update to Database.upsert(), that worked for a while, then started failing. Then I changed Database.upsert() to just upsert, that worked for a single test run, then started failing. Then I carried out the test data setup from the test method to it's own method, annotated with @testSetup. Worked fine until an hour ago, now getting the same error.
Note that everything is working fine in the online org, I can create/edit/delete any and all of the records and the trigger gets called when it's needed and everything executes according to the superbadge requirements with absolutely no problem. I'm at a complete loss.
Here's my test class, took a lot of things out of it for now. Please don't hesitate to ask for additional information, if necessary. Full error message posted below the class.
@isTest
public class TestMaintenaceRequest {
@testSetup
static void setTestData(){
Vehicle__c veh = new Vehicle__c(Name = 'Test Vehicle');
Product2 eq = new Product2(Name = 'Generator', Maintenance_Cycle__c = 230);
insert veh;
insert eq;
Case cs = new Case(Status = 'New',
Origin = 'Web',
Subject = 'Test Case',
Type = 'Repair',
Vehicle__c = veh.Id,
Equipment__c = eq.Id,
Date_Reported__c = Date.today());
insert cs;
Work_Part__c wp = new Work_Part__c(Maintenance_Request__c = cs.Id,
Equipment__r = eq);
insert wp;
}
@isTest
static void testMaintenanceTriggerPositive(){
Case cs = [SELECT Id, Subject, Type, Status, Origin,
Vehicle__c, Equipment__c, Date_Reported__c,
Vehicle__r.Id, Vehicle__r.Name, Equipment__r.Id
FROM Case WHERE Status = 'New'];
System.debug(String.valueOf(cs));
Test.startTest();
cs.Status = 'Closed';
Database.UpsertResult result = Database.upsert(cs, true);
Test.stopTest();
}
}
20:52:17:195 EXCEPTION_THROWN [26]|System.DmlException: Upsert failed. First exception on row 0 with id 5003X00001nKPfMQAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MaintenanceRequest: execution of AfterUpdate