Les Hazlewood Les Hazlewood - 3 months ago 18
Java Question

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

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

Answer

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.