如何在我的春季启动测试中启用jdbc模板

时间:2019-03-15 16:10:31

标签: spring-boot cucumber spring-test

我有一个使用@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: {}

2 个答案:

答案 0 :(得分:0)

JdbcTemplate实际上可以像documented in the Spring Boot docs一样@Autowired@Inject

如果无法立即使用,请检查pom.xml中托管依赖项的版本冲突。

无需手动使用

@EnableAutoConfiguration,因为它已经包含在@SpringBootApplication中。还是您本身没有Application类,例如因为您有仅测试的工件?

答案 1 :(得分:0)

尝试在测试代码中添加 @RunWith(SpringJUnit4ClassRunner.class)