Search code examples
javatestingclass-constantssap-commerce-cloud

Test classes with common behavior


I have to write some integration test classes in the Hybris Commerce Suite and most of them share a common behavior to set up the system (Site, Store, Catalog, Country, ...) or to perform some common action like create a customer.

I created an abstract class that perform all the initialization with constant values in the @Before method and with some common methods like createDefaultCustomer().

All my test classes inherit from this class.

The constant values are separated in different constant classes like

abstract class AbstractTest {
  protected static final class USER_CONSTANTS {
  };
  protected static final class CATALOG_CONSTANTS {
  };
  protected UserModel createDefaultUser() {
  }
}

Now, in order to test, in my subclasses i can do

createDefaultUser();
UserData userData = userFacade.getUserById(USER_CONSTANTS.ID);
assertEquals(USER_CONSTANTS.ID, userData.getId());

If I don't do this there is a lot of duplication in test classes.

My doubt is whether this is acceptable because the abstract class tends to be long and rich of methods or I need to change the design. I would to avoid the creation of separate classes for each group of constants.


Solution

  • I think its perfectly ok to use a base test class for your common code and then extending that in each of the Test class. In fact the test strategy should be in-line with the design of the project code. I am sure that your project module would have some common code as well, your common test section basically map to those functionality of project code.

    I have done similar design in couple of projects without any issue.

    So, whenever your project has a change in common functionality, it would affect test case in common test class, while specific changes go with specific test class.