How do you create a Hazelcast instance embedded in-process/in-memory, without networking?
Asked Answered
S

6

12

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);
Sophistic answered 12/1, 2013 at 3:15 Comment(0)
B
13

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);
Bogie answered 3/5, 2016 at 10:7 Comment(1)
Re-awarding this answer as it seems to be a better alternative than what was available years ago. Thanks for contributing it!Sophistic
P
7

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.

Pamella answered 8/12, 2014 at 14:16 Comment(2)
Link to BasisCacheTest is dead.Greave
Thx. Fixed the link.Pamella
S
2

Answering my own question:

It appears that this isn't possible, but there is a good solution for unit testing purposes which I mention below. Looking at Hazelcast's source code, it appears that networking code is automatically executed upon Node construction, and no amount of Config manipulation worked for me. I'd love to be shown otherwise if possible.

In any event, I was able to accomplish what I needed for unit testing:

As a long time user of EasyMock, I wasn't aware how to cleanly test the code that invoked Hazelcast.newHazelcastInstance(config); since it was a static method call. This is in fact what prompted me to ask this question - I just wanted an in-memory-only Hazelcast instance for testing. I did not want network operations to be attempted on our restricted build machine - I wasn't aware if the machine was restricted enough such that Hazelcast's discovery logic might fail the build.

I then found PowerMock's extension to EasyMock, allowing me to mock static method calls.

With EasyMock and PowerMock, I was able to fully unit test all Hazelcast-related code in our project without actually starting a Hazelcast environment.

Sophistic answered 14/1, 2013 at 20:43 Comment(0)
D
2

I was able to do the same with Mockito. For example, with a Topic:

HazelcastInstance hazelcastInstance = Mockito.mock(HazelcastInstance.class);
Mockito.when(hazelcastInstance.getTopic("yourtopic")).thenReturn(Mockito.mock(ITopic.class));
Dordogne answered 9/9, 2013 at 19:20 Comment(0)
P
0

You can create in memory without mock like this:

HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();

Other params in your constructor you can use mock, this way your code will work well.

Penicillium answered 6/9, 2016 at 15:22 Comment(0)
M
0

You can use HazelcastTestInstance, as described here: https://jukusoft.com/2017/01/11/java-hazelcast-junit-tests/

(Link is German, but code is Java)

Monde answered 10/1, 2017 at 23:43 Comment(1)
Link shows: "This site can’t provide a secure connection" in Chrome.Greave

© 2022 - 2024 — McMap. All rights reserved.