0

如何使用 jCache 和 HazelcastProvider 实现缓存单元测试?

目前我收到此错误:

Caused by: java.lang.IllegalStateException: Unable to connect to any address in the config! The following addresses were tried: [localhost/127.0.0.1:5701, localhost/127.0.0.1:5702, localhost/127.0.0.1:5703]

简化代码如下:

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource("classpath:provider.properties")
@ContextConfiguration(classes = {
    CustomCacheConfiguration.class
})

我在 hazelcast.xml 文件中声明了缓存以及多播,所以我理解正确,我应该有一个嵌入式实例。

//编辑

这是 hazelcast.xml 文件:

<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.hazelcast.com/schema/config
                           http://www.hazelcast.com/schema/config/hazelcast-config-3.8.xsd"
       xmlns="http://www.hazelcast.com/schema/config">
    <group>
        <name>test</name>
        <password>test</password>
    </group>
    <network>
        <join>
            <multicast enabled="true"/>
        </join>
     </network>

    <!-- cache definitions -->
</hazelcast>

这是日志:

Jun 28, 2017 10:55:45 AM com.hazelcast.client.config.XmlClientConfigLocator
INFO: Loading 'hazelcast-client-default.xml' from classpath.
Jun 28, 2017 10:55:45 AM com.hazelcast.client.config.XmlClientConfigLocator
INFO: Loading 'hazelcast-client-default.xml' from classpath.
Jun 28, 2017 10:55:45 AM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [dev] [3.8.2] HazelcastClient 3.8.2 (20170518 - a60f944) is STARTING
Jun 28, 2017 10:55:46 AM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [dev] [3.8.2] HazelcastClient 3.8.2 (20170518 - a60f944) is STARTED
Jun 28, 2017 10:55:46 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5701 as owner member
Jun 28, 2017 10:55:46 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5702 as owner member
Jun 28, 2017 10:55:46 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5703 as owner member
Jun 28, 2017 10:55:46 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
WARNING: hz.client_0 [dev] [3.8.2] Unable to get alive cluster connection, try in 2940 ms later, attempt 1 of 2.
Jun 28, 2017 10:55:49 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5703 as owner member
Jun 28, 2017 10:55:49 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5702 as owner member
Jun 28, 2017 10:55:49 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
INFO: hz.client_0 [dev] [3.8.2] Trying to connect to [127.0.0.1]:5701 as owner member
Jun 28, 2017 10:55:49 AM com.hazelcast.client.spi.impl.ClusterListenerSupport
WARNING: hz.client_0 [dev] [3.8.2] Unable to get alive cluster connection, try in 2996 ms later, attempt 2 of 2.
Jun 28, 2017 10:55:52 AM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [dev] [3.8.2] HazelcastClient 3.8.2 (20170518 - a60f944) is SHUTTING_DOWN
Jun 28, 2017 10:55:52 AM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [dev] [3.8.2] HazelcastClient 3.8.2 (20170518 - a60f944) is SHUTDOWN
4

1 回答 1

0

我想我找到了解决办法。

hazelcast-test.xml使用以下 XML 创建了新文件:

<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.hazelcast.com/schema/config
                           http://www.hazelcast.com/schema/config/hazelcast-config-3.9.xsd"
       xmlns="http://www.hazelcast.com/schema/config">

    <properties>
        <property name="hazelcast.shutdownhook.enabled">false</property>
    </properties>

    <network>
        <join>
            <multicast enabled="false"/>
            <tcp-ip enabled="false"/>
        </join>
    </network>

    <import resource="classpath:hazelcast-cache-declarations.xml"/>
</hazelcast>

然后在测试中我添加了这个:

@BeforeClass
public void init() {
    Config config = new ClasspathXmlConfig("hazelcast-test.xml");
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
}

@AfterClass
public void clean() {
    Hazelcast.shutdownAll();
}
于 2017-06-29T14:13:42.500 回答