Write and execute Integration Tests in Hybris
1. Overview
Integration testing I&T is the phase of testing where individual modules are combined and tested together as a group, as well as where the data transfer between modules is tested carefully.
In this article, we will explore how to write and how to execute integration tests in Hybris.
2. Implementation
2.1. Prepare environment
Integration test requires access both to the database and the Hybris platform resources and modules, hence we will activate the junit tenant first, so our integration tests will run inside the junit tenant context and isolated from other tenants.
To activate junit system run the following Ant command inside the platform directory.
ant yunitinit
You have to make sure that your extensions are visible to the junit tenant, to do so :
- Run Hybris server.
- Open the hac.
- Navigate to Platform -> Tenants.
- Click view in front of the junit tenant.
- Make sure your extensions are checked (if it’s not check them and click initialize tenant).
2.2 Write Integration Test
Write a Java class extend ServicelayerTransactionalTest.java
or ServicelayerBaseTest
and annotated with @IntegrationTest
.
@IntegrationTest
public class CustomMoldeServiceIntegrationTest extends ServicelayerBaseTest {
}
Add a Java public method annotated with @Test
and write the test scenario inside it.
@IntegrationTest
public class CustomModelServiceIntegrationTest extends ServicelayerTransactionalTest {
@Resource
private ModelService modelService;
@Test
public void save_title_model_test() {
// Create new TitleModel
final TitleModel titleModel = modelService.create(TitleModel.class);
titleModel.setCode("toto");
titleModel.setName("toto");
// Save the titleModel
modelService.save(titleModel);
// Assert that the titleModel is saved
Title title = modelService.getSource(titleModel);
TitleModel savedTitleModel = modelService.get(title);
Assert.assertNotNull(savedTitleModel.getPk());
}
}
2.3. Run Test
1. Build your project by running ant all.
2. Then Right-click and RunAs|JunitTest on the CustomModelServiceIntegrationTest.java
using your preferred code editor.
3. If everything goes fine the test well be green.
4. To run all the integrations test of a given extension, run ant integrationtests inside the extension.
3. Conclusion
Integration tests are very fundamental for large scale projects like Hybris, by using them we can insure a good quality of integration between the different modules of our project.
Software Craftsmanship, Stackextend author and Full Stack developer with 6+ years of experience in Java/Kotlin, Java EE, Angular and Hybris…
I’m Passionate about Microservice architectures, Hexagonal architecture, Event Driven architecture, Event Sourcing and Domain Driven design (DDD)…
Huge fan of Clean Code school, SOLID, GRASP principles, Design Patterns, TDD and BDD.
java.lang.IllegalStateException: no tenant active. if you do not want to use tenants, call Registry.activateMasterTenant() to assure the Master tenant is active.
at de.hybris.platform.core.Registry.getCurrentTenant(Registry.java:811) ~[coreserver.jar:?]
could you please help me how to resolve this issue
Hi, make sure to apply all the instructions of the 2.1.Prepare environment section.
In which extension I need to write the class [core or storefront or facades]