在 gradle 测试命令行中使用 -Dspring.config.name 和 -Dspring.config.location

Rav*_*dra 0 testing spring gradle spring-boot

我能够在我的 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 测试上下文没有问题

小智 5

-Dspring.config.location可能是错误的。如果您从命令行运行 jar 文件,您会尝试

$ java -jar myproject.jar --spring.config.location=classpath:app-test.properties
Run Code Online (Sandbox Code Playgroud)

或者你可以在测试类中设置:

@TestPropertySource(locations = "classpath:app-test.properties")
public class TestClass1{...}
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助!

参考文献:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html