Reputation: 403
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
Reputation: 1822
There are some things you need to know about the test system.
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
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
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
To import data not covered by the ServicelayerTest methods, I recommend one of two approaches.
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. importCsv(String pathToFile, String encoding)
in ServicelayerTest
provides you the opportunity to import those. Upvotes: 2