3

I've got a test that passes when run by itself, but fails when run alongside another method in the same class.

Test results

No changes were made between test runs, and I have made sure to select Disable Parallel Apex Testing. Regardless, I get this error:

The exception in the failed run is: System.DmlException: Upsert failed. First exception on row 0 with id a1D8A000000Ci1XUAS; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

The test code is here:

@isTest
private class Debug_VFxTestFailure {
    /**
    * @description Sets up data for test methods
    */
    @TestSetup
    static void setup() {
        List<Account> accts = VFx_TestData.createAccounts(1);
        insert accts;

        VFx_TestData.insertUser('test1@VFx_VolunteerActivities_Test.test', 'Donor on Platform');

        // insert an activity
        List<Volunteer_Activity__c> activities = VFx_TestData.createVolunteerEvents(accts[0], 2);
        insert activities;
    }

    // this test will pass when run alone, but fail when run along with testDestroyAllowed()
    @isTest 
    static void testSaveUpdateAllowed() {
        User donorUser = 
            [SELECT Id FROM User WHERE Username = 'test1@VFx_VolunteerActivities_Test.test' LIMIT 1];

        Volunteer_Activity__c activity = [SELECT Id, Name, RecordType.DeveloperName FROM Volunteer_Activity__c LIMIT 1];

        Boolean exceptionCaught = false;

        try {
            System.runAs(donorUser) {
                // this is actually what I want to test, but for the sake of clarity let's just do basic DML
                //(Volunteer_Activity__c)VFx_VolunteerActivities.save(activity));
                upsert activity;
            }    
        } catch (DMLException e) {
            exceptionCaught = true;
            System.debug('e: ' + e); 
            System.debug('e.getCause(): ' + e.getCause()); 
            System.debug('e.getStackTraceString(): ' + e.getStackTraceString()); 
        }

        System.assertEquals(false, exceptionCaught, 'A Donor on Platform user can update a Volunteer Activity.');
        //System.assertEquals('Something else', activity.Name);
    }

    @isTest 
    static void testDestroyAllowed() {
        User sysAdmin = VFx_TestData.getUserForProfile('System Administrator');
        Volunteer_Activity__c activity = [SELECT Id FROM Volunteer_Activity__c LIMIT 1];

        Boolean exceptionCaught = false;

        try {
            System.runAs(sysAdmin) {
                // this is actually what I want to test, but for the sake of clarity let's just do basic DML
                //VFx_VolunteerActivities.destroy(activity); 
                delete activity;
            }    
        } catch (Exception e) {
            exceptionCaught = true;
        }

        System.assertEquals(false, exceptionCaught, 'A System Adminstrator can delete a Volunteer Activity.');
    }
}

What am I missing?

  • I'm not 100% sure but have you tried without the recordtype.developername? remove that from your activity query and give it another shot. You might also consider moving your query where you pull in the activity into the runas block as this will help test sharing rules and what not for that user. – gNerb Apr 1 '17 at 3:39
  • Try with only creating test tecord in the setup. You are creating 2 records yet query later with a limit of 1 without any sort. Could just be the wrong one is being returned? We may need to see your test utility method – Eric Apr 1 '17 at 8:48
  • are there multiple triggers on Activity? They may be executing in variable order – cropredy Apr 1 '17 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.