【问题标题】:Using -Dspring.config.name and -Dspring.config.location in gradle test command line在 gradle 测试命令行中使用 -Dspring.config.name 和 -Dspring.config.location
【发布时间】:2015-01-01 13:01:01
【问题描述】:

我可以在我的 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 Boot 中它可以完美运行。

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

【问题讨论】:

  • 这是什么意思:它似乎对我不起作用。
  • 我认为您还需要明确说明您使用的命令(您可以设置一个系统属性但不能设置另一个系统属性没有任何意义,除非它们在上下文中实际上不同)。

标签: spring testing gradle spring-boot


【解决方案1】:

-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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-07
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2018-11-03
    • 2021-11-27
    • 1970-01-01
    相关资源
    最近更新 更多