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

时间:2015-01-01 13:01:01

标签: spring testing 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未被弹簧加载以加载属性文件。但是在春季靴子里它完全有效。

提到我可以传递其他系统属性并在我的测试类中使用。意味着在gradle中没有问题将系统属性传递给junit测试上下文

1 个答案:

答案 0 :(得分:4)

-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