Testing a component

This tutorial focuses on writing unit tests for the input component that was created in this previous tutorial.

This tutorial covers:

  1. How to load components in a unit test.

  2. How to create a job pipeline.

  3. How to run the test in standalone mode.

The test class is as follows:

public class HazelcastMapperTest {

    public static final SimpleComponentRule COMPONENTS = new SimpleComponentRule(HazelcastMapperTest.class
                                                                                    .getPackage().getName()); (1)

    private static HazelcastInstance instance; (2)

    public static void startInstanceWithData() { (3)
        instance = Hazelcast.newHazelcastInstance();
        final IMap<Object, Object> map = instance.getMap(HazelcastMapperTest.class.getSimpleName());
        IntStream.range(0, 100).forEach(i -> map.put("test_" + i, "value #" + i));

    public static void stopInstance() { (4)

    public void run() { (5)
        Job.components() (6)
                .component("source", "Hazelcast://Input?configuration.mapName=" + HazelcastMapperTest.class.getSimpleName())
                .component("output", "test://collector")

        final List<JsonObject> outputs = COMPONENTS.getCollectedData(JsonObject.class); (7)
        assertEquals(100, outputs.size());
1 SimpleComponentRule is a JUnit rule that lets you load your component from a package. This rule also provides some test components like emitter and collector. Learn more about JUnit in this section.
2 Using an embedded Hazelcast instance to test the input component.
3 Creating an embedded Hazelcast instance and filling it with some test data. A map with the name of the test class is created and data is added to it.
4 Cleaning up the instance after the end of the tests.
5 Defining the unit test. It first creates a job pipeline that uses our input component.
6 The pipeline builder Job is used to create a job. It contains two components: the input component and the test collector component. The input component is connected to the collector component. Then the job is built and ran locally.
7 After the job has finished running. The COMPONENTS rule instance is used to get the collected data from the collector component. Once this is done, it is possible to do some assertion on the collected data.
Scroll to top