Write and execute Integration Tests in Hybris

Hybris Logo

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).

Add extension to junit tenant in Hybris

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.

Successful integration test in Hybris

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.

 

2.5 2 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ramesh
Ramesh
5 years ago

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

hari
hari
5 years ago

In which extension I need to write the class [core or storefront or facades]

3
0
Would love your thoughts, please comment.x
()
x