You need to sign in to do that
Don't have an account?
Frans Flippo 14
Test data isolation not working
I've got a test class that starts:
The loop that prints out existing products prints nothing.
However, the insert fails with an error:
It's true that such a record already exists in my org. However, the tests are supposed to not see that data. So why is this insert failing?
@IsTest private class WarehouseCalloutServiceTest { @TestSetup public static void setup() { List<Product2> existingProducts = [ SELECT Id, StockKeepingUnit FROM Product2 ]; System.debug('Existing products:'); for (Product2 product : existingProducts) { System.debug('- ' + product); } // Insert equipment List<Product2> equipmentList = new List<Product2>(); // Maintenance cycle changes equipmentList.add(new Product2( Name = 'Generator 1000 kW', Cost__c = 5000, Current_Inventory__c = 6, StockKeepingUnit = '100003', Lifespan_Months__c = 0, Maintenance_Cycle__c = 730, Replacement_Part__c = false )); // No changes equipmentList.add(new Product2( Name = 'Cooling Fan', Cost__c = 300, Current_Inventory__c = 183, StockKeepingUnit = '100004', Lifespan_Months__c = 0, Maintenance_Cycle__c = 0, Replacement_Part__c = true )); // Quantity changes equipmentList.add(new Product2( Name = 'Fuse 20A', Cost__c = 22, Current_Inventory__c = 144, StockKeepingUnit = '100005', Lifespan_Months__c = 0, Maintenance_Cycle__c = 0, Replacement_Part__c = true )); // SKU 100006 doesn't exist yet System.debug('Inserting ' + equipmentList); insert equipmentList; }
The loop that prints out existing products prints nothing.
However, the insert fails with an error:
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, A Equipment with this SKU already exists.: [StockKeepingUnit]
It's true that such a record already exists in my org. However, the tests are supposed to not see that data. So why is this insert failing?
Per the documentation, the following example shows how to create test records once and then access them in multiple test methods.
As you can see in the code fragment I posted, using @TestSetup to set up test data is exactly what I'm doing.
But the test is already failing there: it refuses to insert even the first Product2 record in the list because of a supposed duplicate value; but the test shouldn't be seeing any data already in Product2, so that means a single insert should never fail for duplication reasons: you can't have a duplicate if there's only one record.
It seems that this data isolation for tests isn't as good as it should be.
Anyone else that can shed some light on this?
Thanks,
Frans
Unit Testing problem with Product2 records: A Product with this SKU already exists (https://salesforce.stackexchange.com/questions/290542/unit-testing-problem-with-product2-records-a-product-with-this-sku-already-exis)
Thanks, Leonardo. That looks like the exact same issue.
I do think that this points to a definite bug in Salesforce, or at the very least a misrepresentation of how this "test data isolation" works, e.g. on:
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_access.htm
It's at least odd that Salesforce Customer Support employees like Anudeep don't know about these bugs/limitations.
I'll raise a support ticket on my company's Salesforce contract and get an official response. At the very least these limitations need to be mentioned in the documentation.