Search code examples
javadistributed-computinghazelcast

TestHazelcastFactory client mock is not working


I am trying to use TestHazelcastFactory to make the Mocked hazelcast client to connect to Mocked HazelCast intance.Here is the code i have tried

Server

TestHazelcastFactory factory = new TestHazelcastFactory();
 HazelcastInstance instance= factory.newHazelcastInstance(config);

Client

TestHazelcastFactory factory = new TestHazelcastFactory();
HazelcastInstance instance= factory.newHazelcastClient(clientConfig);

But it not able to connect to the mocked Hazelcast Instance.


Solution

  • First of all, you need to have hazelcast-tests.jars and hazelcast-client-tests.jar

    testCompile "com.hazelcast:hazelcast:$HZ_VERSION:tests" testCompile "com.hazelcast:hazelcast-client:$HZ_VERSION:tests"

    Next, you need to use same TestHazelcastFactory to clear HazelcastInstance for the members and the clients.

    Check this snippet

        TestHazelcastFactory testHazelcastFactory = new TestHazelcastFactory();
    
        HazelcastInstance member = testHazelcastFactory.newHazelcastInstance();
        IMap<Object, Object> testMap1 = member.getMap("test");
        testMap1.put(1, "test");
    
        HazelcastInstance client = testHazelcastFactory.newHazelcastClient();
        IMap<Object, Object> testMap2 = client.getMap("test");
        Object o = testMap2.get(1);
        System.out.println("o = " + o);
    

    Let me know if you have any questions.

    Cheers,

    Vik