【问题标题】:Unit Testing Using embedded Cassandra in Spring Boot在 Spring Boot 中使用嵌入式 Cassandra 进行单元测试
【发布时间】:2018-11-27 15:40:15
【问题描述】:

我正在尝试测试一个 Spring Boot 应用程序,其中包括保存到 cassandra DB 中。 对于单元测试,我使用 EmbeddedCassandra。

这些是依赖项。

compile group: "com.datastax.cassandra", name: "cassandra-driver-core", version:"2.1.7"
    compile group: "com.datastax.cassandra", name: "cassandra-driver-dse", version:"2.1.7"
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile  ("org.cassandraunit:cassandra-unit-spring:2.1.9.2"){
        exclude group:"org.cassandraunit",module:"cassandra-unit"
    }

这些是我用过的注释

@RunWith(SpringRunner.class)
@WebMvcTest(value = MyController.class)
@Import(value = {MyDao.class})
@ContextConfiguration
@TestExecutionListeners(listeners = {
        CassandraUnitDependencyInjectionTestExecutionListener.class,
        CassandraUnitTestExecutionListener.class,
        ServletTestExecutionListener.class,
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class}
)
@EmbeddedCassandra(timeout = 60000)
@CassandraDataSet(keyspace = "test", value = {"test.cql"})

但是在单元测试中保存的值是填充原始的 cassandra db 。 谁能帮我解决这个问题?

【问题讨论】:

    标签: spring-boot testing junit cassandra


    【解决方案1】:

    Cassandra 单元使用: native_transport_port: 9142,但 spring boot 需要 9042 端口,因此您应该在配置中更改端口或使用 cassandra.yaml 文件。 作为替代方案,您可以看看这个:

    https://github.com/nosan/embedded-cassandra/blob/master/README.adoc#spring-boot

    它还提供了@Cql 注释以在每次测试之前或/之后执行 cql 脚本。

    希望对你有用。

    【讨论】:

    • 我试过这个。仍然引发错误,显示键空间'realkeyspace'不存在。它是实际的键空间。
    • 你用过@CassandraDataSet(keyspace = "test",..),你能在'realkeyspace'上改变'test'吗?
    猜你喜欢
    • 2016-12-07
    • 2017-07-30
    • 2018-08-06
    • 2017-07-11
    • 2017-10-21
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 2020-09-05
    相关资源
    最近更新 更多