使用Hazelcast Provider测试jCache

时间:2017-06-28 08:43:17

标签: spring testing junit hazelcast jcache

如何使用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文件中声明了缓存以及多播,因此我理解正确,我应该有一个嵌入式实例。

// EDIT

这是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

1 个答案:

答案 0 :(得分:0)

我想我找到了解决方案。

我使用以下XML创建了新的hazelcast-test.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();
}