我没有在我的beandefinition类中添加cassandra模板bean,但它工作正常并在运行时给我所需的输出,但是在编写junit测试类时 它给我一个错误"没有名为&cassandraTemplate'可用"
为什么在运行我的junit测试类时会出现此问题。
这是我的代码:
@Configuration
@PropertySource("cassandra.properties")
@EnableCassandraRepositories(basePackages = "...repository")
public class Beandef
{
@Autowired
public Environment environment;
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
@Bean(name = "clusterFactory")
public CassandraClusterFactoryBean getCluster() {
PoolingOptions poolingOptions = new PoolingOptions();
cluster.setContactPoints(environment.getProperty("cassandra.contactpoints"));
cluster.setPoolingOptions(poolingOptions);
cluster.setPort(Integer.parseInt(environment.getProperty("cassandra.port")));
poolingOptions.setNewConnectionThreshold(HostDistance.LOCAL, 50);
return cluster;
}
@Bean
@DependsOn("clusterFactory")
public CassandraSessionFactoryBean getSession() throws Exception {
CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
session.setCluster(cluster.getObject());
session.setKeyspaceName(environment.getProperty("cassandra.keyspace"));
session.setConverter(new MappingCassandraConverter(new CassandraMappingContextAware()));
session.setSchemaAction(SchemaAction.NONE);
return session;
}
答案 0 :(得分:1)
如果您正在使用Spring Boot,并且您的测试类没有在类路径中提供@SpringBootApplication,则必须添加@EnableAutoConfiguration以启用Spring Boot自动配置(即使使用spring-boot-starter-依赖项中的data-cassandra。)