我正在尝试使用TestHazelcastFactory使Mocked hazelcast客户端连接到Mocked HazelCast intance.Here是我尝试过的代码

服务器

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


客户

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


但是它无法连接到模拟的Hazelcast实例。

最佳答案

首先,您需要具有hazelcast-tests.jarshazelcast-client-tests.jar

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

接下来,您需要使用相同的TestHazelcastFactory清除成员和客户端的HazelcastInstance

检查此片段

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


如果您有任何问题,请告诉我。

干杯,

维克

10-08 13:37