我想測試使用cassandra作爲CrudRepository的SpringBoot應用程序。我結束了SpringBoot:使用Cassandra進行單元測試
/*
* 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());
}
}
與
<dependency>
<groupId>org.cassandraunit</groupId>
<artifactId>cassandra-unit-spring</artifactId>
<version>3.0.0.1</version>
<scope>test</scope>
</dependency>
和
CREATE TABLE MY_CASSANDRA_ENTRY (
MY_CASS_STRING varchar PRIMARY KEY
)
這導致
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))
如果我使用的是舊版本卡桑德拉單元彈簧
<dependency>
<groupId>org.cassandraunit</groupId>
<artifactId>cassandra-unit-spring</artifactId>
<version>2.1.9.2</version>
<scope>test</scope>
</dependency>
它以NullPointerException結束,因爲值回購沒有被注入。
來源https://github.com/StephanPraetsch/spring.boot.cassandra.unit.test