This is a prerelease version.

View latest

Set up dependencies and tests

Hazelcast provides the support classes in the main distribution JAR. These classes are made available to the classpath by including the tests classifier in pom.xml:

    <dependency>
      <groupId>com.hazelcast</groupId>
      <artifactId>hazelcast</artifactId>
      <version>{hz.version}</version>
      <classifier>tests</classifier>
    </dependency>

In this section, we will use JUnit (either 4 or 5) for code examples. Setup for other test frameworks follows the same steps for setting up JUnit 5.

JUnit4 setup

For JUnit4, the following dependencies must be included:

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>{junit4.version}</version>
      <scope>test</scope>
    </dependency>
   <dependency>
      <groupId>org.opentest4j</groupId>
      <artifactId>opentest4j</artifactId>
      <version>{opentest4j.version}</version>
      <scope>test</scope>
   </dependency>

JUnit5 setup

When using JUnit5, the following dependencies are required:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>

With the following extra dependencies:

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.20.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.20.0</version>
        </dependency>

Example usage

A typical way to create instances is:

Config config = HazelcastTestSupport.regularInstanceConfig();
// setup config
int clusterSize = 2;
TestHazelcastFactory factory = new TestHazelcastFactory(clusterSize);
HazelcastInstance[] members = factory.newInstances(config, clusterSize);
// ...
factory.shutdownAll(); // or for each member: members[i].shutdown();

Run the tests

You can run tests within an IDE or using mvn test with Apache Maven. Instances created using the test support classes are automatically configured with the mock network.

By supplying the system property -Dhazelcast.test.use.network=true, the tests are run with fully configured members, skipping the use of the mock network:

mvn test -Dhazelcast.test.use.network=true