我有一个Spring Boot应用程序,并且在应用程序和之前的JUNIT测试中运行良好。
我最近开始使用JDBCTemplate和DataSource,所有测试都无法启动。
stacktrace的主要问题是,一旦尝试自动装配数据源或jdbc模板,它似乎无法找到数据源属性
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'directoryLoader': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.core.JdbcTemplate com.millerhare.winfsapi.fsloader.DirectoryLoader.jdbcTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
...
这是Autowire类
@Component
public class DirectoryLoader {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
DataSource ds;
但是在应用程序和测试的application.properties中,所有数据源属性都已设置
spring.datasource.url=jdbc\:oracle\:thin\:@xxxx
spring.datasource.username=xxxx
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
我已经尝试更改其他属性,例如server.port,这些属性已被选中,因此很明显找到了application.properties文件。
我测试的原始配置是
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WinFsapiApplication.class)
@WebIntegrationTest
我也尝试通过@TestPropertiesSource
设置属性,但这也找不到它们。
我错过了什么?
答案 0 :(得分:0)
为什么数据源URL中有反斜杠?字符串应为spring.datasource.url=jdbc:oracle:thin:@xxxxx
。我相信你有一个\
的事实导致驱动程序的自动检测(指定URL应该足以检测驱动程序类,你不需要指定它)。
答案 1 :(得分:0)
我遇到了类似的问题,春季启动应用程序运行良好,我可以从Postman测试它,但Junits失败了。将application.properties添加到src / test / resources为我解决了它。我知道spring boot应该从src / main / resources中获取它,但即使我要求它在我的配置文件中使用@PropertySource(“classpath:application.properties”)专门查找它也不行。