我能够在我的 Spring Boot 应用程序中成功使用 -Dspring.config.name
和 -Dspring.config.location
。现在想使用相同的 -Dspring.config.location
传递给 Gradle 测试任务。它似乎不适合我。尽管我可以传递其他系统属性并在我的测试类中使用。
我想用它在不同的环境中运行我的测试。我知道可以使用 Spring 配置文件。
任何帮助将不胜感激
编辑:使用 -Dspring.config.name=app-test
和/或 -Dspring.config.location=classpath:/conf/app-test.properties
没有被 Spring 拾取以加载属性文件。但在 spring boot 中它运行良好。
提到我可以传递其他系统属性并在我的测试类中使用。这意味着在 gradle 中将系统属性传递给 junit 测试上下文没有问题
最佳答案
-Dspring.config.location 可能有误。 如果你从命令行运行 jar 文件,你会尝试
$ java -jar myproject.jar --spring.config.location=classpath:app-test.properties
或者你可以在测试类中设置:
@TestPropertySource(locations = "classpath:app-test.properties")
public class TestClass1{...}
希望对您有所帮助!
引用文献:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
关于spring - 在 gradle 测试命令行中使用 -Dspring.config.name 和 -Dspring.config.location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27731031/