简体   繁体   English

SpringBoot:使用Cassandra进行单元测试

[英]SpringBoot: Unit Test with Cassandra

I'd like to test my SpringBoot application that uses cassandra as a CrudRepository. 我想测试使用cassandra作为CrudRepository的SpringBoot应用程序。 I ended up with 我结束了

/*
 * https://github.com/jsevellec/cassandra-unit/wiki/Spring-for-Cassandra-unit
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan
@ContextConfiguration(value = { "classpath:/default-context.xml" })
@TestExecutionListeners({ CassandraUnitTestExecutionListener.class })
@CassandraDataSet(value = { "setupTables.cql" }, keyspace = "keyspaceToCreate")
@CassandraUnit
public class ApplicationTests {

    @Autowired
    MyCassandraRepository repo;

    @Test
    public void contextLoads() {

        System.out.println(repo.findAll());

    }

}

with

    <dependency>
        <groupId>org.cassandraunit</groupId>
        <artifactId>cassandra-unit-spring</artifactId>
        <version>3.0.0.1</version>
        <scope>test</scope>
    </dependency>

and

CREATE TABLE MY_CASSANDRA_ENTRY (
  MY_CASS_STRING varchar PRIMARY KEY
)

This leads to 这导致

com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9142 (com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured table schema_keyspaces))

If I use an older version of cassandra-unit-spring 如果我使用较旧版本的cassandra-unit-spring

    <dependency>
        <groupId>org.cassandraunit</groupId>
        <artifactId>cassandra-unit-spring</artifactId>
        <version>2.1.9.2</version>
        <scope>test</scope>
    </dependency>

it ends with a NullPointerException because the value repo is not injected. 它以NullPointerException结束,因为未注入值repo。

Sources https://github.com/StephanPraetsch/spring.boot.cassandra.unit.test 来源https://github.com/StephanPraetsch/spring.boot.cassandra.unit.test

CassandraUnit starts on port 9142 . CassandraUnit从端口9142开始。 Spring Boot defaults to port 9042 . Spring Boot默认为端口9042 You need to set the port and the keyspace name so the Cassandra driver can pickup the right connection details. 您需要设置端口和键空间名称,以便Cassandra驱动程序可以获取正确的连接详细信息。

You need to change two things in your test: 您需要在测试中更改两件事:

  1. Please use @SpringBootTest instead of @EnableAutoConfiguration . 请使用@SpringBootTest而不是@EnableAutoConfiguration That enables a couple of other things like configuration property support which you will need in step 2. 这样可以在步骤2中执行配置属性支持等其他一些操作。

  2. Create src/test/resources/application.properties and set the port and keyspace name. 创建src/test/resources/application.properties并设置端口和键空间名称。

spring.data.cassandra.port=9142
spring.data.cassandra.keyspace-name=keyspaceToCreate

This will configure the correct port and keyspace. 这将配置正确的端口和密钥空间。

Alternatively, you could specify properties using 或者,您可以使用指定属性

@SpringBootTest({"spring.data.cassandra.port=9142",
                 "spring.data.cassandra.keyspace-name=keyspaceToCreate"})

I suppose version 3.x of cassandra-unit-spring is incompatible for me. 我想cassandra-unit-spring版本3.x对我来说是不兼容的。 Better 2.x. 更好的2.x.

With 2.x there is an error log 使用2.x时会出现错误日志

15:10:23.366 [pool-1-thread-1] ERROR org.apache.cassandra.service.CassandraDaemon - cassandra.jmx.local.port missing from cassandra-env.sh, unable to start local JMX service.null
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest({ "spring.data.cassandra.port=9142",
        "spring.data.cassandra.keyspace-name=keyspaceToCreate" })
@EnableAutoConfiguration
@ComponentScan
@ContextConfiguration
@TestExecutionListeners({ CassandraUnitDependencyInjectionTestExecutionListener.class,
        DependencyInjectionTestExecutionListener.class })
@CassandraDataSet(value = { "setupTables.cql" }, keyspace = "keyspaceToCreate")
@CassandraUnit
public class ApplicationTests {

    @Autowired
    MyCassandraRepository repo;

    @Test
    public void contextLoads() {

        System.out.println(repo.findAll());

    }

}

and

    <dependency>
        <groupId>org.cassandraunit</groupId>
        <artifactId>cassandra-unit-spring</artifactId>
        <version>2.1.9.2</version>
        <scope>test</scope>
    </dependency>

works. 作品。

But if upgrade to 但如果升级到

    <dependency>
        <groupId>org.cassandraunit</groupId>
        <artifactId>cassandra-unit-spring</artifactId>
        <version>3.0.0.1</version>
        <scope>test</scope>
    </dependency>

again I get the error 我又得到了错误

com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9142 (com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured table schema_keyspaces))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM