Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting the Environment Variables with version 1.18.0 not working with Junit 5 #70

Open
visha-r opened this issue Aug 14, 2018 · 12 comments

Comments

@visha-r
Copy link

visha-r commented Aug 14, 2018

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
}
@stefanbirkner
Copy link
Owner

stefanbirkner commented Aug 21, 2018

Does this work with System Rules 1.17.2 and JUnit Lambda (JUnit 5)?

@visha-r
Copy link
Author

visha-r commented Aug 22, 2018

Yes it works with System Rules 1.17.2 and Junit 5.

@realtec
Copy link

realtec commented Jan 29, 2019

Same problem here, 1.80.0 and 1.19.0 doesn't work but 1.17.2 does.

@artem-emelin
Copy link

artem-emelin commented May 2, 2019

Same for me, 1.17.2 works but 1.19.0 doesn't for JUnit 5.3.2

@wojtekk
Copy link

wojtekk commented 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
Copy link

vxrahn commented Apr 2, 2020

Using 1.17.2 works but throws

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?

@hq6
Copy link

hq6 commented 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
Copy link

Jupiter support available in https://github.com/webcompere/system-stubs/tree/master/system-stubs-jupiter - a fork of system-lambda

@uklimaschewski
Copy link

Any chance this is getting fixed for?

Java 17 (Temurin) and junit 4, using 1.17.2:

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)

@ashleyfrieze
Copy link

@uklimaschewski #70 (comment)

System Stubs is essentially compatible with System Rules (package names have changed) and does support later Java.

@uklimaschewski
Copy link

@ashleyfrieze Thanks, good to know!

@ashleyfrieze
Copy link

@ashleyfrieze Thanks, good to know!

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants