I'd like to test my SpringBoot application that uses cassandra as a CrudRepository. 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
<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.
Sources https://github.com/StephanPraetsch/spring.boot.cassandra.unit.test
CassandraUnit starts on port 9142
. Spring Boot defaults to port 9042
. You need to set the port and the keyspace name so the Cassandra driver can pickup the right connection details.
You need to change two things in your test:
Please use @SpringBootTest
instead of @EnableAutoConfiguration
. That enables a couple of other things like configuration property support which you will need in step 2.
Create src/test/resources/application.properties
and set the port and keyspace name.
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. Better 2.x.
With 2.x there is an error log
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.