【问题标题】:NoHostAvailableException when Cassandra Embedded is used使用 Cassandra Embedded 时出现 NoHostAvailableException
【发布时间】:2019-01-29 19:57:45
【问题描述】:

我有一个使用 spring-data-cassandra 的 Spring Boot 应用程序。我正在尝试使用嵌入式 Cassandra 为这个应用程序编写集成测试。

这是我使用的依赖项:

testCompile('org.springframework.boot:spring-boot-starter-test') {
       exclude group: 'junit'
   }
   testCompile("org.junit.jupiter:junit-jupiter-api:5.3.2")
   testRuntime("org.junit.jupiter:junit-jupiter-engine:5.3.2")
   testCompile("org.mockito:mockito-junit-jupiter:2.23.4")
   testCompile group: 'org.cassandraunit', name: 'cassandra-unit-spring', version: '3.5.0.1'

这里是测试代码:

import org.cassandraunit.spring.CassandraDataSet;
import org.cassandraunit.spring.CassandraUnitTestExecutionListener;
import org.cassandraunit.spring.EmbeddedCassandra;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ContextConfiguration
@TestExecutionListeners(
        listeners = CassandraUnitTestExecutionListener.class,
        mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
@CassandraDataSet(value = "dataset.cql", keyspace = "key_space")
@EmbeddedCassandra
@ActiveProfiles("test")
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class IntegrationTest {

    @Test
    public void test() {
        System.out.println("Test");
    }
}

但是,当我运行应用程序时,出现以下错误:

com.datastax.driver.core.exceptions.NoHostAvailableException

这意味着默认的 Cassandra 嵌入式配置不起作用!

【问题讨论】:

    标签: java spring-boot cassandra junit5 spring-data-cassandra


    【解决方案1】:

    我不是cassandra-unit 的专家,但我可以向您推荐一个替代库:https://github.com/nosan/embedded-cassandra

    @SpringBootTest
    @EmbeddedCassandra(scripts = "...", replace = EmbeddedCassandra.Replace.NONE /*use auto-configured cluster */)
    @ExtendWith(SpringExtension.class)
    public class EmbeddedCassandraTests {
    
        @Test
        public void testMe() {
    
        }
    
    }
    

    希望对你有帮助。

    【讨论】:

    • @Dymtro 谢谢。是的,我看到了这个库,但我不想使用它,因为它不是 Cassandra 的官方库。
    猜你喜欢
    • 2016-06-30
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2018-01-18
    • 2021-02-07
    • 2015-04-16
    相关资源
    最近更新 更多