cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to create a session cart using Mockito?

Former Member
0 Kudos
390

I am using hybris 6.2 and trying to write Unit test cases using Mockito. In one scenario I should mock a session cart. Please help how can I mock the same.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi ,

If I understood your problem correctly, then the answer to your question is as follows: Cart is implemented with CartModel class (search in the Javadoc for more information: https://help.hybris.com/6.2.0/api/commercesuite/index.html).

Following this article: https://help.hybris.com/6.6.0/hcd/8c6e8668866910148fc390638f82bad2.html I’ve created an example mock.

 //Create mock object
 final CartModel mockedCartModel = mock(CartModel.class);
 
 //Mock the method return value
 when(mockedCartModel.getDescription()).thenReturn("Mocked description");
 
 //Test the result
 assertEquals("The call to getDescription() should've returned 'Mocked description'", "Mocked description", mockedCartModel.getDescription()); 

If I misunderstood your question, please correct me, and I will take another look at it.

Hope this helps and Best Regards,

Former Member
0 Kudos

I think the description I have provided is not enough to give the correct answer. Here are the more details. 1. When I call a method like public void doB2BUnitChange(B2BUnit b2bUnit) then It should change the current user B2BUnit. But before changing the B2BUnit it should remove the session cart if is exist. 2. After removing the session cart I should assert the same in test method. But the method return type is void.

Thank you for the reply.

Former Member
0 Kudos

Hi ,

I think you might use implementation of B2BCartService which is extending CartService (default one being DefaultB2BCartService) and utilize its methods such as:

 public boolean hasSessionCart();
 public CartModel getSessionCart();

Hope this helps and Best Regards,