Rachit
Rachit

Reputation: 403

How to mock model objects in SAP Hybris?

while writing integration tests in SAP Hybris, I am getting exceptions which imply that the model objects are not available during the test case.

It seems like normally the ImpEx which run normally during initialization are not running here.It is becoming hectic creating objects using model service.

Is there some other way?What about the custom objects defined by me in the product(like ABCProduct extending Product) and also values for them?Is it possible to mock them as well?What about BaseSite and PriceRow?

Upvotes: 0

Views: 1346

Answers (1)

Johannes von Zmuda
Johannes von Zmuda

Reputation: 1822

There are some things you need to know about the test system.

Tenants

Usually you work with the master tenant. The test system however has its own tenant called junit. A tenant is meant to be kind of like a separate data set for the hybris server running the same code. That way you can run different shops on the same infrastructure but every shop can only access the data that is meant for the tenant. How does it work? Every tenant has a table prefix, only the tenant master has an empty one. So the products table for the master tenant is called "products", but the "products" table for the junit tenant is called "junit_products".

Further reading: https://help.sap.com/viewer/d0224eca81e249cb821f2cdf45a82ace/1905/en-US/8c14e7ae866910148e59ebf4a2685857.html

Initialization

When you use the initialization using ant initialize or the admin console, you usually only initialize the master tenant. When you want to initialize the junit tenant, you need to either change to the junit tenant in the admin console or run ant initialize -Dtenant=junit. However this creates only the most basic data.

More on how to execute initialization in admin console in section "Executing Tests": https://help.sap.com/viewer/d0224eca81e249cb821f2cdf45a82ace/1905/en-US/aae25ecb74ab4bd69cc5270ffd455459.html

Creating test data

There are a few classes you can inherit from, to create an integration test, but only ServicelayerTest provides methods to create sample data. All those methods import impex files located in /hybris/bin/platform/ext/core/resources/servicelayer/test/

createCoreData() Creates languages, currencies, units etc. See: testBasics.csv

createDefaultCatalog() Creates a sample product catalog with an online catalog version and basic sample products. See: testCatalog.csv

createHardwareCatalog() Creates a sample product catalog with staged and online version, products and classifications. See testHwcatalog.csv and testClassification.csv

createDefaultUsers() Creates sample customers with addresses etc. See testUser.csv

Importing custom data

To import data not covered by the ServicelayerTest methods, I recommend one of two approaches.

  1. Using ModelService and other services to create your data. E.g. you can use the OrderService to create sample orders. You can as well create utility classes that provide you with creating sample data. You can wire every service you need by annotating it with the @Resource annotation.
  2. Using impex files to create all data you need. You can split those up into different files that serve different needs (e.g. customers, orders, products...). The method importCsv(String pathToFile, String encoding) in ServicelayerTest provides you the opportunity to import those.

Upvotes: 2

Related Questions