【发布时间】:2017-12-09 01:39:20
【问题描述】:
我是 Spring boot 的新手,但这是我现在面临的问题:
// Application.java
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
private Cluster cluster = null;
@PostConstruct
private void migrateCassandra() {
Database database = new Database(this.cluster, "foo");
MigrationTask migration = new MigrationTask(database, new MigrationRepository());
migration.migrate();
}
}
所以基本上,我正在尝试引导一个 Spring 应用程序,然后进行一些 cassandra 迁移。
我还为我的用户模型定义了一个存储库:
// UserRepo.java
public interface UserRepo extends CassandraRepository<User> {
}
现在我正在尝试使用以下简单的测试用例来测试我的 repo 类:
// UserRepoTest.java
@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(replace = Replace.NONE)
@DataJpaTest
public class UserRepoTest {
@Autowired
private UserRepo userRepo = null;
@Autowired
private TestEntityManager entityManager = null;
@Test
public void findOne_whenUserExists_thenReturnUser() {
String id = UUID.randomUUID().toString();
User user = new User();
user.setId(id);
this.entityManager.persist(user);
assertEquals(this.userRepo.findOne(user.getId()).getId(), id);
}
@Test
public void findOne_whenUserNotExists_thenReturnNull() {
assertNull(this.userRepo.findOne(UUID.randomUUID().toString()));
}
}
我希望测试能够通过,但我收到一条错误消息,提示“没有可用的 'com.datastax.driver.core.Cluster' 类型的合格 bean”。看起来 spring 无法自动装配 cluster 对象,但这是为什么呢?我该如何解决?非常感谢!
【问题讨论】:
-
在你的代码中哪里可以看到类集群的bean(接口集群的实现)??
-
一种可能的解决方案:删除这两行:
@Autowired private Cluster cluster = null; -
我没有定义Class Cluster的bean,应该是spring-boot-starter-data-cassandra提供的。如果我运行我的应用程序,它就可以工作。
-
那么测试配置中缺少某些东西
标签: java spring spring-mvc cassandra