Skip to content

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

Open

Description

@visha-r

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
}

Activity

  1. stefanbirkner commented on Aug 21, 2018

    @stefanbirkner
    Owner

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

  2. visha-r commented on Aug 22, 2018

    @visha-r
    Author

    Yes it works with System Rules 1.17.2 and Junit 5.

  3. realtec commented on Jan 29, 2019

    @realtec

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

  4. artem-emelin commented on May 2, 2019

    @artem-emelin

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

  5. wojtekk commented on Jul 5, 2019

    @wojtekk

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

  6. vxrahn commented on Apr 2, 2020

    @vxrahn

    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?

  7. hq6 commented on Apr 11, 2020

    @hq6

    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.

  8. ashleyfrieze commented on Nov 18, 2020

    @ashleyfrieze

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

  9. uklimaschewski commented on Mar 22, 2023

    @uklimaschewski

    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)
    
  10. ashleyfrieze commented on Mar 22, 2023

    @ashleyfrieze

    @uklimaschewski #70 (comment)

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

  11. uklimaschewski commented on Mar 23, 2023

    @uklimaschewski

    @ashleyfrieze Thanks, good to know!

  12. ashleyfrieze commented on Mar 23, 2023

    @ashleyfrieze

    @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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions