I'm trying to create some Apex tests.
Of course I am NOT using SeeAllData=true
.
When I try to insert some instances of Product2, even though I've inserted none before, I get:
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, A Product with this SKU already exists.: [StockKeepingUnit] Class.ProductBundleControllerIntTest: line 48, column 1
If I try the following just before the attempted insert:
System.debug('######### EXISTING PRODUCT: ' + [
SELECT Id, StockKeepingUnit
FROM Product2
WHERE StockKeepingUnit != NULL
]);
I get:
17:58:23.557 (3561926690)|USER_DEBUG|[47]|DEBUG|######### EXISTING PRODUCT: ()
Even if I do the following to absolutely guarantee that no previous records could possibly exist and that I am inserting no duplicates, I get the same error:
delete [
SELECT Id, StockKeepingUnit
FROM Product2
];
Map<String, Product2> product2BySkuMap = new Map<String, Product2>();
for (Product2 product2 : TEST_PRODUCT_LIST) {
product2BySkuMap.put(product2.StockKeepingUnit, product2);
}
insert product2BySkuMap.values();
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, A Product with this SKU already exists.: [StockKeepingUnit]
Further Notes:
This is an absolutely fresh sandbox, so there really is no data for it to match or to avoid matching.
Also at play within the functionality being tested are Custom Metadata which also needs to have values matching these SKU values. So even if we could change the SKU for the test, we can't (and shouldn't) change it for the Custom Metadata.
What could I be missing? How can I fix this?