在 Springboot、Gradle 和 SpringBootTest 中设置环境变量?

问题描述

我正在运行单元测试,底层代码依赖于一些环境变量的存在。 不是应用程序属性,而是实际的系统环境变量。如果它们不存在,应用就会死机。

Gradle 测试块

test {
    useJUnitPlatform()
    testLogging {
        exceptionFormat "full"
        showExceptions = true
        showStackTraces = true
        showStandardStreams = true
    }
    environment "APP_ID","id"
    environment "STATSD_HOST","localhost"
    environment "STATSD_PORT","8125"
}

单元测试

@SpringBoottest
@TestPropertySource(value = "classpath:application-test.properties")
public class MyCoolTest {
...

但是我仍然得到了关于这个值的 NPE。如果我在 intellij IDE 中明确强制使用 env 变量,这会起作用,但我不想那样做。有什么建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)