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

标签 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没有被 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/

相关文章:

java - 方法抛出 'org.hibernate.PersistentObjectException' 异常。分离的实体传递到持久化

java - 在 Spring 集成测试期间刷新/重建特定 beans

function - react 组件中的测试方法不起作用

angularjs - 我将如何使用 AngularJs 模拟 Jasmine 的 then() 响应测试

testing - 如何测试 Bit-Banged 通信的汇编例程

android - Android Studio Gradle错误 “Multiple dex files define…”

java - Spring Neo4j 保存 MultipartFile

java - 有一个实现和 "service"类 : Are these best practice?

java - maven 和 gradle 构建中的依赖 jar 版本不同

gradle - gradle可以使用latest.release作为默认依赖版本吗?