【发布时间】:2015-04-19 01:17:11
【问题描述】:
我想为运行 unitTest 提供与使用默认生产数据库不同的数据库。我考虑过使用配置文件来解决这个问题。 这是spring4启动项目,所以所有的东西都有注释。 这就是我正在做的:
在 src/main/resources 下,我输入了application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/services
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
在 src/test/resources 下,我放了 application-test.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/services_test
spring.datasource.username=postgres
spring.datasource.password=Hercules1
spring.datasource.driver-class-name=org.postgresql.Driver
然后,我在测试前放了@ActiveProfiles("test"),现在当我运行单元测试时,我立即遇到了这个错误:
java.lang.IllegalStateException: 无法加载 ApplicationContext
我google了很多,没有什么可以解决这个错误。
你能指出我的解决方案有什么问题吗?
谢谢
【问题讨论】:
-
如果您所做的只是在 test 和 main 中使用不同的属性,那么不要打扰配置文件。只需将文件命名为
application.properties并将其放入src/test/resources。假设您使用的是 Maven,该目录中的资源将覆盖src/main/resources中的资源。 -
请分享整个错误
标签: java spring spring-boot