我有一个使用@SpringBootTest
的Cucumber测试框架代码,在我的测试类中,我想配置JdbcTemplate
以连接到数据库。我无法autowire
。如果我使用@EnableAutoConfiguration
,它似乎可以正常工作,但还会看到许多其他错误。有没有一种方法可以启用我可以用来启用jdbc模板
@SpringBootTest(classes = AppTestConfiguration.class)
@EnableEncryptableProperties
public class CucumberContextConfiguration {
}
@Configuration
@ComponentScan(basePackages = { "com.*" })
public class AppTestConfiguration {
}
我看到的错误是
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
答案 0 :(得分:0)
JdbcTemplate
实际上可以像documented in the Spring Boot docs一样@Autowired
或@Inject
。
如果无法立即使用,请检查pom.xml
中托管依赖项的版本冲突。
@EnableAutoConfiguration
,因为它已经包含在@SpringBootApplication
中。还是您本身没有Application类,例如因为您有仅测试的工件?
答案 1 :(得分:0)
尝试在测试代码中添加 @RunWith(SpringJUnit4ClassRunner.class)
。