Java系统规则EnvironmentVariables变量在设置一些值后为空
我必须在我的 uniTtest 中设置一些环境变量和属性值,我遵循 https://stefanbirkner.github.io/system-rules/
中定义的示例,
这就是我的类的样子:
import org.junit.Rule;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EnvironmentVariablesTest
{
@Rule
public final EnvironmentVariables environmentVariables
= new EnvironmentVariables();
@Test
public void setEnvironmentVariable() {
environmentVariables.set("name", "value");
assertEquals("value", System.getenv("name"));
}
}
但我遇到了下一个错误:
org.opentest4j.AssertionFailedError:
Expected :value
Actual :null
我还在我的 pom.xml 文件中添加了下一个依赖项。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<exclusions>
<exclusion>
<!-- using junit:junit instead -->
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
</exclusion>
</exclusions>
</dependency>
我无法弄清楚这个例子中的错误是什么,或者我错过了什么。
谢谢!
I have to setup some environmental variables and properties values in my uniTtest, I'm following the example defined in https://stefanbirkner.github.io/system-rules/
It is how my class looks like:
import org.junit.Rule;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EnvironmentVariablesTest
{
@Rule
public final EnvironmentVariables environmentVariables
= new EnvironmentVariables();
@Test
public void setEnvironmentVariable() {
environmentVariables.set("name", "value");
assertEquals("value", System.getenv("name"));
}
}
But I'm getting the next error:
org.opentest4j.AssertionFailedError:
Expected :value
Actual :null
I also added the next dependencies in my pom.xml file.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<exclusions>
<exclusion>
<!-- using junit:junit instead -->
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
</exclusion>
</exclusions>
</dependency>
I can't figure out what is the error in this example, or what I'm missing.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了完全相同的问题。它适用于版本 1.17.2,但到 1.18.0 或 1.19.0 时,环境变量变为空。
我还不知道为什么。不过,将版本降级到 1.17.2 就可以了。
I ran into exactly the same issue. It worked for version 1.17.2, but going to 1.18.0 or 1.19.0, the environment variables become null.
I don't know why yet. Downgrading your version to 1.17.2 will work though.