Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.

JMock

JMock

Elastic Path uses the JMock framework to facilitate testing a single class in isolation. JMock eases the creation of mock objects, which are test object instances that a class under test can reference. By using mocks, you can control the output that a class under test receives from objects it depends on so that you can test specific situations. Furthermore, mock objects can be given expectations of which methods will be invoked on it by the class under test. These expectations can optionally specify the parameters that are passed into the method as well as the number of times the method is invoked.

JMock is used in conjunction with JUnit, so you create your mock objects inside a JUnit test case. If a mock object's expectations are not met, the JUnit test case fails.

Using JMock

The following code shows a typical JMock2 mock object usage pattern. In this example, we test the Product class and we use a mock ProductType object to test Product's interaction with ProductType.

@Before
public void setUp() {
    context = new JUnit4Mockery();
}

@test
public void testProductInteractionWithProductType(){
    Product productImpl = new ProductImpl();
    
    //Create a mock object to mock a product type
    final ProductType mockProductType = context.mock(ProductType.class);
    
    //Pass the mock object to the product class
    productImpl.setProductType(mockProductType);
    
    //Create the object that the mock object returns
    AttributeGroup expectedAttributeGroup = new AttributeGroupImpl();
    
    //Specify that the product type's getProductAttributeGroup() method must
    //be called one time and that it will return to the product the attribute
    //group we wish to test the product with
    context.checking(new Expectations() {
            {
                oneOf(mockProductType).getProductAttributeGroup();
                will(returnValue(expectedAttributeGroup));
            }
        });
        
    //Invoke the method being tested, which will interact with the mock object
    AttributeGroup returnedAttributeGroup = productImpl.getAttributeGroup();
    
    //Check the results of the operation here
    assertAttributeGroupsAreEqual(expectedAttributeGroup, returnedAttributeGroup);
}

Primitive types must be specified exactly in Jmock constraints

Jmock is very strict regarding types in constraints. For example, take the get() method siganature in the PersistencEngine class shown below:

Persistence get(final Class persistenceClass, final long uidPk) throws EpPersistenceException;

This method cannot be mocked as shown below because the "1" that is passed to equals is an int instead of a long.

allowing(mockPersistenceEngine).get(with(equal(Brand.class)), with(equal(1)));
will(returnValue(brand1));

Instead, the method must be mocked with the type explicitly stated as shown below.

allowing(mockPersistenceEngine).get(with(equal(Brand.class)), with(equal(1L)));
will(returnValue(brand1));