Cassandra 单元 spring - 每个测试类仅启动一次嵌入式 Cassandra
我正在使用 cassandra-unit-spring maven 依赖项为我的 spring-boot 应用程序设置集成测试。我能够运行调用 spring-boot 应用程序的测试,该应用程序进而访问内存中的嵌入式 Cassandra 数据库。
下面是我的测试类的代码。
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({CassandraUnitDependencyInjectionTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})
@CassandraDataSet(value = "cassandra/dbcreate.cql", keyspace = "test")
@EmbeddedCassandra
@SpringBootTest({"spring.data.cassandra.port=9142"})
public class IntegrationTest {
@Autowired
private TestRepository testRepository;
@Test
public void testFindById() {
Token token = generateRandomToken();
testRepository.insert(token);
Optional<Token> tokenStored = testRepository.findById(token.getKey());
compareReplayToken(token, tokenStored.get()); //This method does the assertions
}
}
此单个测试调用嵌入式 Cassandra 并根据 cassandra/dbcreate.cql 文件中的命令创建键空间和表。测试运行后,键空间和表将被删除。
到现在为止,还好。但是,如果我尝试在此类中添加多个测试,则此方法会在每个测试开始时创建键空间和表,然后在测试运行后删除它们。
dbcreate.cql 文件有很多命令来创建多个表,当这些命令为每个测试运行时,这使得我的测试非常慢。
另外,当我尝试拥有多个此类测试类时,这个问题会成倍增加。
我能想到的可能的解决方案是:
为每个测试类提供一个单独的 cql 文件,该文件仅包含与该类相关的有限 cql 命令 - 同样,这并不能解决数据库重置的问题单个类中的每个测试
我想运行所有集成测试来一次启动这个嵌入式 Cassandra,并且表和键空间应该只创建和删除一次以实现快速执行
对于这样的问题,理想的解决方案应该是什么? 非常感谢任何帮助。 谢谢!
I am setting up integration tests for my spring-boot application using cassandra-unit-spring maven dependency. I am able to run my tests which invoke the spring-boot application which in turn accesses an in-memory embedded Cassandra database.
Below is the code for my test class
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({CassandraUnitDependencyInjectionTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})
@CassandraDataSet(value = "cassandra/dbcreate.cql", keyspace = "test")
@EmbeddedCassandra
@SpringBootTest({"spring.data.cassandra.port=9142"})
public class IntegrationTest {
@Autowired
private TestRepository testRepository;
@Test
public void testFindById() {
Token token = generateRandomToken();
testRepository.insert(token);
Optional<Token> tokenStored = testRepository.findById(token.getKey());
compareReplayToken(token, tokenStored.get()); //This method does the assertions
}
}
This single test invokes the embedded Cassandra and creates the keyspace and tables from the commands in the cassandra/dbcreate.cql file. After the test runs, the keyspace and tables are dropped.
Till now, it is fine. But, if I try to add multiple tests in this class, this approach creates the keyspace and tables at the beginning of each test and then drops them once the test runs.
And the dbcreate.cql file has a lot of commands to create multiple tables and when these commands run for each test, this makes my tests really slow.
Also, this problem multiplies when I try to have multiple such test classes.
Possible solution that I could think of is:
Have a separate cql file for each test class that has limited cql commands concerned with that class only - Again, this doesn't solve the problem of the database reset for each test in a single class
I want to run all my integration tests for a single launch of this embedded Cassandra and the tables and keyspace should be created and dropped only once for a fast execution
What should be the ideal solution for such a problem?
Any help is much appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论