Spring Boot Data嵌入式Cassandra

时间:2017-02-13 20:29:26

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

在我的Spring Boot 1.5.1应用程序中,我将为Cassandra相关逻辑编写单元/集成测试。

我添加了以下Maven依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>

默认的Spring Boot Cassandra配置将与真正的Cassandra数据库服务器连接。

为了配置我的测试以使用嵌入式Cassandra服务器,Spring / Spring Boot中是否有任何选项?如果是这样,请您显示所需的配置。

4 个答案:

答案 0 :(得分:6)

我们在项目Cassandra + Spring Boot上使用。 以下是对我们有用的步骤:

a)配置你这样的测试

<!DOCTYPE html>
<html>

<head>
  <title>test</title>
  <style>
    .animator {
      background-image: url(https://upload.wikimedia.org/wikipedia/commons/3/33/Jordansallotments.jpg);
      animation: move-background 2s linear infinite;
      position: absolute;
      width: 100%;
      height: 100%;
    }
    
    html,
    body {
      position: relative;
      width: 100%;
      height: 100%;
    }
    
    @keyframes move-background {
      0% {
        background-position: 0%, 0%;
      }
      100% {
        background-position: 100%, 0%;
      }
    }
  </style>
</head>

<body>
  <div class="animator"></div>
</body>

</html>

b)在你的src / test / resources / application.properties中,添加它(请注意,嵌入式cassandra从端口9142开始,但不是默认9042)

<script type="text/javascript">
    var bdg_img = document.getElementById('bdgimg');
    var animate;
    function moveRight()
    {
        bdg_img.style.left = bdg_img.style.left || 0;
        bdg_img.style.left = parseInt(bdg_img.style.left) + 10 + 'px';
        animate = setTimeout(moveRight,40); // call moveRight in 20msec
    }
    moveRight();
</script>

c)在src / test / resources

中创建空文件bootstrap_test.cql

d)添加到您的pom.xml

import org.cassandraunit.spring.CassandraDataSet;
import org.cassandraunit.spring.CassandraUnitDependencyInjectionTestExecutionListener;
import org.cassandraunit.spring.CassandraUnitTestExecutionListener;
import org.cassandraunit.spring.EmbeddedCassandra;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class)
@TestExecutionListeners(listeners = {
    CassandraUnitDependencyInjectionTestExecutionListener.class,
    CassandraUnitTestExecutionListener.class,
    ServletTestExecutionListener.class,
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class
})
@EmbeddedCassandra(timeout = 60000)
@CassandraDataSet(value = {"bootstrap_test.cql"}, keyspace = "local_test")
public abstract class BaseTest {

这应该足以使用Embedded Cassandra运行测试。 希望它有所帮助。

答案 1 :(得分:2)

Spring Boot中没有嵌入式Apache Cassandra支持,而且没有计划。一方面对嵌入式Apache Cassandra的需求很少,另一方面,Apache Cassandra带来了许多与Boot的其他依赖项冲突的依赖项。

看看Cassandra Unit

使用JUnit测试时,您还可以构建自己的测试规则,使您可以完全控制Apache Cassandra版本,运行时行为等。看一下可能的实现:CassandraRule.java

答案 2 :(得分:1)

我知道这是一个老话题了,但是有两种方法可以将嵌入式Cassandra与Spring结合使用。

1。第一种方法是使用Cassandra Unit Spring

我已经创建了一个示例应用程序,您可以检出以下Github存储库。

注意 :嵌入式服务器在 9142 端口上启动,而不在9042端口上启动!

2。第二种方法是使用 maven-plugin

第1步:

将以下Maven插件添加到 pom.xml

public static void TestMethodWrapper(int? x = 10, int? y = 20) {
    if ((x == null) && (y == null)) {
        return TestMethod();
    } else if (y == null) {
        return TestMethod(x);
    }
    return TestMethod(x, y);
}

第2步:

在项目的根级别

创建 cassandra / cql 目录。 嵌入式Cassandra服务器将从该目录中初始化您的数据库。

重要:该目录中的所有文件均为 .cql 文件。

示例:cassandra / cql / load.cql

第3步:

创建集成测试。

重要提示: surfire 插件仅将带有 IT 后缀的测试类解释为集成测试。

步骤4:

mvn验证 以运行集成测试

答案 3 :(得分:1)

OP提供的解决方案不再适用于最新版本的Spring Boot,因为CassandraUnitDependencyInjectionTestExecutionListener来自cassandra-unit-spring,仅适用于JUnit 4,并且世界已经迁移到JUnit 5。

我创建了一个cassandra-unit-spring库,该库启动Cassandra服务器并使端口可用作Spring Boot环境属性。