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

后端 未结 6 749
深忆病人
深忆病人 2021-02-14 02:09

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

相关标签:
6条回答
  • 2021-02-14 02:15

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

    (Link is German, but code is Java)

    0 讨论(0)
  • 2021-02-14 02:20

    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);
    
    0 讨论(0)
  • 2021-02-14 02:24

    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));
    
    0 讨论(0)
  • 2021-02-14 02:25

    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.

    0 讨论(0)
  • 2021-02-14 02:25

    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.

    0 讨论(0)
  • 2021-02-14 02:36

    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.

    0 讨论(0)
提交回复
热议问题