stefanbirkner / system-rules

A collection of JUnit rules for testing code which uses java.lang.System.
http://stefanbirkner.github.io/system-rules
Other
546 stars 71 forks source link

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

Open visha-r opened 6 years ago

visha-r commented 6 years ago

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 commented 6 years ago

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

visha-r commented 6 years ago

Yes it works with System Rules 1.17.2 and Junit 5.

realtec commented 5 years ago

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

artem-emelin commented 5 years ago

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

wojtekk commented 5 years ago

The same for me - 1.17.2 works but 1.18.0 and 1.19.0 does not for JUnit 5.5.0

vxrahn commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

uklimaschewski commented 1 year ago

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 commented 1 year ago

@uklimaschewski https://github.com/stefanbirkner/system-rules/issues/70#issuecomment-729623890

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

uklimaschewski commented 1 year ago

@ashleyfrieze Thanks, good to know!

ashleyfrieze commented 1 year ago

@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.