CartExtension.CartTestUtil

ISTEST

A utility class that can be only used when writing Apex tests. It allows you to get a CartExtension.Cart Apex object, similar to the one that would be sent by the extensibility framework.

Fields 

Name of the records created by the createCart() method. 

  • global ACCOUNT_NAMEString
  • global CARTITEM_NAMEString
  • global CART_NAMEString
  • global DELIVERYGROUP_NAMEString
  • global WEBSTORE_NAMEString

Methods 

global static Cart getCart(ID cartRecordId) 

Allows you to retrieve a CartExtension.Cart Apex object from a WebCart record ID.

Parameters 

ParamDescription
cartRecordIdID of the WebCart record to load as a CartExtension.Cart Apex object.

Returns 

TypeDescription
CartCartExtension.Cart

Example 

1static void testMyCustomCalculator() {
2    // Given
3    final String accountName = 'My Test Account';
4    final String webStoreName = 'My Test WebStore';
5    final String cartName = 'My Test Cart';
6    final Id personAccountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('PersonAccount').getRecordTypeId();
7    Schema.Account testAccount = new Schema.Account(LastName=accountName, RecordTypeId=personAccountRecordTypeId);
8    insert testAccount;
9    Schema.WebStore testWebStore = new Schema.WebStore(Name=webStoreName);
10    insert testWebStore;
11    Schema.Account account = [SELECT Id FROM Account WHERE id=:testAccount.Id LIMIT 1];
12    Schema.WebStore webStore = [SELECT Id FROM WebStore WHERE id=:testWebStore.Id LIMIT 1];
13    Schema.WebCart testCart = new WebCart(Name=cartName, WebStoreId=webStore.Id, AccountId=account.Id);
14    insert testCart;
15    Schema.WebCart cart = [SELECT Id FROM WebCart WHERE Name=:cartName LIMIT 1];
16    CartExtension.Cart cartObject = CartExtension.CartTestUtil.getCart(cart.Id);
17    CartExtension.CartCalculateCalculatorRequest request = new CartExtension.CartCalculateCalculatorRequest(cartObject);
18    MyCustomCalculator calculator = new MyCustomCalculator();
19    // When
20    Test.startTest();
21    calculator.calculate(request);
22    Test.stopTest();
23    // Then
24    // some assertions
25}

global static Cart createCart() 

Creates the necessary Account, WebStore (type B2B), WebCart, CartDeliveryGroup, and CartItem records and returns the corresponding CartExtension.Cart Apex object.

Returns 

TypeDescription
CartCartExtension.Cart

Example 

1static void testMyCustomCalculator() {
2    // Given
3    CartExtension.Cart cartObject = CartExtension.CartTestUtil.createCart();
4    CartExtension.CartCalculateCalculatorRequest request = new CartExtension.CartCalculateCalculatorRequest(cartObject);
5    MyCustomCalculator calculator = new MyCustomCalculator();
6    // When
7    Test.startTest();
8    calculator.calculate(request);
9    Test.stopTest();
10    // Then
11    // some assertions
12}

global static Cart createCart(WebStoreTypeEnum webStoreType) 

Creates the necessary Account, WebStore, WebCart, CartDeliveryGroup, and CartItem records and returns the corresponding CartExtension.Cart Apex object.

Parameters 

ParamDescription
webStoreTypetype of the WebStore the cart will be created for

Returns 

TypeDescription
CartCartExtension.Cart

Example 

1static void testMyCustomCalculator() {
2    // Given
3    CartExtension.Cart cartObject = CartExtension.CartTestUtil.createCart(CartExtension.WebStoreTypeEnum.B2C);
4    CartExtension.CartCalculateCalculatorRequest request = new CartExtension.CartCalculateCalculatorRequest(cartObject);
5    MyCustomCalculator calculator = new MyCustomCalculator();
6    // When
7    Test.startTest();
8    calculator.calculate(request);
9    Test.stopTest();
10    // Then
11    // some assertions
12}

Newer Version Available

This content describes an older version of this product. View Latest

DID THIS ARTICLE SOLVE YOUR ISSUE?
Let us know so we can improve!