You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Setting the Environment Variables with version 1.18.0 not working with Junit 5 #70
I am trying to set the environment variables using the version 1.18.0 and I am always getting null. I am using Junit 5 and have no issues using 1.17.2 version to set the system environment variables. I need to set different environment values for different tests, so not setting the value right after creating the rule. My code looks like the following:
@Rule
private final EnvironmentVariables environmentVariables = new EnvironmentVariables();
@Test
void testEnvValues() {
environmentVariables.set("service_env", "dev");
assertEquals("dev", System.getenv("service_env")); // always fails
}
Reacted by David M. Carr, Alexander Ionov, tirumalesh-yeligar-by-zz and Udo Klimaschewski
Yes it works with System Rules 1.17.2 and Junit 5.
Reacted by Ben, Roi Rav-Hon, Dhiraj Bhatt, Dionny Santiago, David Valentine, Sebastian J., Eduardo Fonseca, Max Rydahl Andersen, David M. Carr, Spyros Papadopoulos and 3 more
Reacted by Ben, Dionny Santiago and Spyros Papadopoulos
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.junit.contrib.java.lang.system.EnvironmentVariables to field java.util.Collections$UnmodifiableMap.m
WARNING: All illegal access operations will be denied in a future release
Is there any solution currently in progress to avoid this error?
Also working with 1.17.2 (with the above warnings) when using JUnit 5. 1.19 does not work in the sense that environmental variables are still NULL after being set.
java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Map java.util.Collections$UnmodifiableMap.m accessible: module java.base does not "opens java.util" to unnamed module @50cbc42f
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
at org.junit.contrib.java.lang.system.EnvironmentVariables.getFieldValue(EnvironmentVariables.java:147)
at org.junit.contrib.java.lang.system.EnvironmentVariables.getEditableMapOfVariables(EnvironmentVariables.java:109)
at org.junit.contrib.java.lang.system.EnvironmentVariables.access$000(EnvironmentVariables.java:36)
at org.junit.contrib.java.lang.system.EnvironmentVariables$EnvironmentVariablesStatement.restoreOriginalVariables(EnvironmentVariables.java:93)
at org.junit.contrib.java.lang.system.EnvironmentVariables$EnvironmentVariablesStatement.evaluate(EnvironmentVariables.java:84)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:258)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
Java 17 (Temurin) and junit 4, using 1.19.0:
java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Map java.util.Collections$UnmodifiableMap.m accessible: module java.base does not "opens java.util" to unnamed module @61a52fbd
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
at org.junit.contrib.java.lang.system.EnvironmentVariables.getFieldValue(EnvironmentVariables.java:188)
at org.junit.contrib.java.lang.system.EnvironmentVariables.getEditableMapOfVariables(EnvironmentVariables.java:150)
at org.junit.contrib.java.lang.system.EnvironmentVariables.access$200(EnvironmentVariables.java:49)
at org.junit.contrib.java.lang.system.EnvironmentVariables$EnvironmentVariablesStatement.restoreOriginalVariables(EnvironmentVariables.java:134)
at org.junit.contrib.java.lang.system.EnvironmentVariables$EnvironmentVariablesStatement.evaluate(EnvironmentVariables.java:125)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:258)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
It wasn't my intention to replace this tooling. I wanted a JUnit 5 plugin that worked a certain way. I've also offered the JDK17 compatible solution to whoever wants to steal the code from my repo. That said, it comes at a price, which is the adoption of mockito-inline.
Activity
stefanbirkner commentedon Aug 21, 2018
Does this work with System Rules 1.17.2 and JUnit Lambda (JUnit 5)?
visha-r commentedon Aug 22, 2018
Yes it works with System Rules 1.17.2 and Junit 5.
realtec commentedon Jan 29, 2019
Same problem here, 1.80.0 and 1.19.0 doesn't work but 1.17.2 does.
artem-emelin commentedon May 2, 2019
Same for me, 1.17.2 works but 1.19.0 doesn't for JUnit 5.3.2
wojtekk commentedon Jul 5, 2019
The same for me - 1.17.2 works but 1.18.0 and 1.19.0 does not for JUnit 5.5.0
vxrahn commentedon Apr 2, 2020
Using 1.17.2 works but throws
Is there any solution currently in progress to avoid this error?
hq6 commentedon Apr 11, 2020
Also working with 1.17.2 (with the above warnings) when using JUnit 5. 1.19 does not work in the sense that environmental variables are still NULL after being set.
ashleyfrieze commentedon Nov 18, 2020
Jupiter support available in https://github.com/webcompere/system-stubs/tree/master/system-stubs-jupiter - a fork of
system-lambdauklimaschewski commentedon Mar 22, 2023
Any chance this is getting fixed for?
Java 17 (Temurin) and junit 4, using 1.17.2:
Java 17 (Temurin) and junit 4, using 1.19.0:
ashleyfrieze commentedon Mar 22, 2023
@uklimaschewski #70 (comment)
System Stubs is essentially compatible with System Rules (package names have changed) and does support later Java.
uklimaschewski commentedon Mar 23, 2023
@ashleyfrieze Thanks, good to know!
ashleyfrieze commentedon Mar 23, 2023
It wasn't my intention to replace this tooling. I wanted a JUnit 5 plugin that worked a certain way. I've also offered the JDK17 compatible solution to whoever wants to steal the code from my repo. That said, it comes at a price, which is the adoption of
mockito-inline.