In my unit tests, I want to create an embedded (in-process/in-memory) Hazelcast instance that does not attempt to start or perform any networking operations at all.
How do I do this?
For example:
Config config = new Config();
// what goes here?
HazelcastInstance inProcessOnly = Hazelcast.newHazelcastInstance(config);
Embedded Mode Hazelcast is written in Java, which means that Java developers can integrate, or embed, it as a library in their applications. This topology is called embedded mode. All you need to do is add the Hazelcast JAR file to the application's classpath and start a member with the Java API.
FWIW I have created a test in Hazelcast 3.6.1 and programmatically disabled the network cluster using the following code in the constructor. This creates a standalone server for testing.
It's not as quick as using a mock but it is faster than accepting the default config.
Config config = new Config();
config.setProperty("hazelcast.shutdownhook.enabled", "false");
NetworkConfig network = config.getNetworkConfig();
network.getJoin().getTcpIpConfig().setEnabled(false);
network.getJoin().getMulticastConfig().setEnabled(false);
HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
You can also use TestHazelcastInstanceFactory which is also used by Hazelcast internally for unit tests. You will need to add this Maven dependency for it:
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>${hazelcast.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
See BasicCacheTest for an example of how it used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With