【发布时间】:2014-02-20 12:44:42
【问题描述】:
架构创建在目标类 SimpleRepo.java 中完成。
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 does not exist”。
@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;
}
}
为什么在运行第二个测试用例时 createSchema() 中创建的键空间会消失?如何解决这个问题?感谢您的指导。
【问题讨论】:
-
如果您显示测试用例代码会有所帮助。