Spring 使用Hazelcast提供程序测试jCache

Spring 使用Hazelcast提供程序测试jCache,spring,testing,junit,hazelcast,jcache,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

如何使用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>

我想我找到了解决办法

我用以下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>

你能把日志寄出去吗?可能是Hazelcast没有开始。严格来说,这不是单元测试,您可能希望使用模拟对象。或者,看看测试是如何在那里运行的。您应该检查类路径和其他bean。hazelcast服务器将使用
hazelcast.xml
文件,要么您没有启动该文件,要么它启动得不够快。您收到的错误消息来自HazelcastClient。客户机正在启动,但找不到要连接的服务器--因此,要么您启动的是客户机而不是服务器,要么是服务器之前的客户机。问题是,我没有在任何地方启动客户机,在测试期间,hazelcast似乎无法加载我的xml文件,因此为客户端加载默认值。
com.hazelcast:hazelcast客户端
取决于
com.hazelcast:hazelcast
-客户端取决于服务器,而不是服务器。因此,服务器代码永远无法启动客户端。但很明显,你的计划中有一些东西是错误的。你能在Github上发布一个完整的例子并链接到这里吗?或者,确保从类路径中删除
com.hazelcast:hazelcast客户端
,然后查看发生了什么。还值得检查一下“hazelcast.jcache.provider.type”集是如何设置的。我想我已经找到了一个解决方案,我把它作为一个答案发布了出来。您有什么建议和顾虑吗?您是在
@Before
而不是
@BeforeClass
中创建Hazelcast实例的,因此如果您有多个
@Test
方法,您将拥有多个。这可以解释错误的原因,但实际上需要上传更多的代码才能发现任何原因。我已经修复了它,感谢您在类前插入
@BeforeClass
注释。我已经改变了答案,现在所有的测试都正常了。
<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();
}