1

If we use ConnectApi.CommerceCart.addItemToCart , exception occured : System.UnsupportedOperationException: ConnectApi methods are not supported in data siloed tests. Which method or class to use to create cartItem to webCart.

2 Answers 2

1

Happy new year!

Most Connect in Apex methods require access to real organization data, and fail unless used in test methods marked @IsTest(SeeAllData=true).

ConnectApi CommerceCart requires access to real org data. In our project, we prepopulate / seed test data per environment for the methods which interacts with ConnectApi Commerce functionalities (entitlements, catalog, buyer groups / accounts) for testing these methods or you can use the real org data in your environment right now.

Once setup is complete, we query for those items along with @IsTest(SeeAllData=true) annotation

/*
 * Your test cart class here...
 *
 * NOTE: 
 * ConnectApi Methods are not supperted in data siloed tests. Please use @IsTest(SeeAllData=true)
 * Test class containing a test setup method cannot have any methods annotated with @IsTest(SeeAllData=true)
 */
@IsTest(SeeAllData=true)
public with sharing class YourWebcartClassTest {
    @IsTest
    private static void testYourMethod(String productId, Integer quantityAdded) {
        // Query for the user org data with the right entitlements, associated to the buyer groups / accounts 
        User entitledUser = [...];

        // Query for the product org data to be added.
        Product2 correctProduct = [...];

        // Product Quantity for demo purposes
        Integer productQuantity = 5;

        System.runAs(entitledUser) {
            // Assuming your addToCart method has two params (productId and quantity which is required in the ConnectApi.CommerceCart.addItemToCart method)
            myCartServiceClass.addToCartMethod(correctProduct.Id, productQuantity);

            // Perform Assertions here..
        }
    }
}
1

This is unfortunately another case when Salesforce product team does not follow their own guidelines. SeeAllData=true should be a last resort. Especially that you might want to create a package which uses ConnectApi and then you cannot reliably expect to know the setup of the target org.

One possible approach: In our ISV packages we use Test.isRunningTest() to skip ConnectApi callouts, but it truly hinders the purpose of unit tests. Also we are yet to pass security review.

I also remember that we had trouble to setup all kinds of data related to Lightning B2B. For example the creation of a Product2 with ProductClass = 'VariationParent' was impossible in api 50.0. Not sure of current state.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .