Exception 当嵌入式Cassandra运行多个测试用例时出现异常:键空间**不存在
模式创建在目标类SimpleRepo.java内完成Exception 当嵌入式Cassandra运行多个测试用例时出现异常:键空间**不存在,exception,junit,cassandra,Exception,Junit,Cassandra,模式创建在目标类SimpleRepo.java内完成 public class SimpleRepo { private Cluster cluster; private Session session; private String keyspace = "app"; private String table = "myTable"; @Autowired public SimpleRepo(Cluster cluster) {
public class SimpleRepo {
private Cluster cluster;
private Session session;
private String keyspace = "app";
private String table = "myTable";
@Autowired
public SimpleRepo(Cluster cluster) {
this.cluster = cluster;
}
@PostConstruct
private void init() {
session = cluster.connect();
createSchema();
}
public void createSchema() {
.....
}
}
当运行SimpleTest.java并在其中包含一个测试用例时,它将通过。当运行两个案例时,只有第一个案例通过,第二个案例抛出异常:“com.datastax.driver.core.exceptions.InvalidQueryException:Keyspace app不存在”
为什么在运行第二个测试用例时,createSchema()中创建的键空间会消失?如何解决这个问题?感谢您的指导。CassandraUnitTestExecutionListener在每次测试后调用cleanServer()。调用EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(),该函数将删除所有非系统键空间
您的代码只在@PostConstruct中创建一次密钥空间,所以只有第一个测试用例可以使用它
看起来您应该使用@CassandraDataSet为每个新测试初始化一个键空间
如果您展示测试用例代码,将会有所帮助。非常感谢您的详细解释。如果键空间不存在,我尝试使用@CassandraDataSet初始化键空间。现在这两种情况都可以通过。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class, SimpleRepo.class})
@TestExecutionListeners({CassandraUnitTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})
@EmbeddedCassandra
public class SimpleTest {
@Autowired
private SimpleRepo simpleRepo;
@Test
public void testSave() throws Exception {
......
}
@Test
public void testDel() throws IOException {
......
}
}
@Configuration
public class TestConfig {
@Bean(destroyMethod = "shutdown")
public Cluster cluster() throws ConfigurationException, TTransportException, IOException, InterruptedException{
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
Cluster cluster = Cluster.builder()
.addContactPoints("127.0.0.1")
.withPort(9142)
.build();
return cluster;
}
}