简体   繁体   中英

how to set the environment variables in junit 5 for test case

i am working on Junit5. My java code uses the System.getenv("demoVar") to access environment variable. so how do i set up this environment variable in the jUnit5 test class, so that my code can access the value of this environment variable during the test.

From this other SO answer https://stackoverflow.com/a/59635733/2185719 :

There is JUnit Pioneer, a "JUnit 5 extension pack".

jUnit Pioneer offers an annotation that sets environment variables for a test. For example:

@Test
@SetEnvironmentVariable(key = "PATH", value = "")
void testPath_isEmpty() {
    assertThat(System.getenv("PATH")).isEmpty();
}

You can't within the actual java process because these environmental values using getenv are immutable .

One way would be to start another vm or another process where you could introduce your new environment value.

Another way would be to switch to System.getProperty , but be sure you understand the differences.

https://www.baeldung.com/java-system-get-property-vs-system-getenv

Here is a little testcode:

public class EnvironmentVarsTest {
    private static int counter = 0;

    @BeforeEach
    public void setUp() {
        counter = counter + 1;
        System.setProperty("simple_test_env_property", String.valueOf(counter));
    }

    @Test
    public void testFirst() {
        printOutValues();
    }

    @Test
    public void testSecond() {
        printOutValues();
    }

    private void printOutValues() {
        System.out.println("--------------------");
        System.out.println("val=" + counter);
        System.out.println("envval=" + System.getProperty("simple_test_env_property"));
    }

}

This can be achieved with https://github.com/webcompere/system-stubs/tree/master/system-stubs-jupiter


@ExtendWith(SystemStubsExtension.class)
class TestClass {

    @SystemStub
    private EnvironmentVariables environmentVariables =
        new EnvironmentVariables("demoVar", "val");

    @Test
    void test() {
        // can use the environment
        assertThat(System.getenv("demoVar")).isEqualTo("val");

        // can also change the environment
        environmentVariables.set("foo", "bar");

        // environment variables restored to previous state when
        // test ends
    }
}

Simple solution if you use Gradle, you can add following to your build.gradle

test {
  environment "ENV_VAR_NAME", "ENV_VAR_VALUE"
}

Link to Gradle doc: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle.api.tasks.testing.Test:environment

The common practice is to use System properties instead of environment variables. In this case you will run your java/maven/gradle command or whatever you use to run your tests with option -D demoVar="{your_value}". for maven goal:

 maven clean install -DdemoVar="test"

for java jar:

 java -jar xxx.jar -DdemoVar="test"

You will be able to get it from code with System.getProperty("demoVar") .

If you really need to use environment variable, use OS functionality. For linux:

demoVar="test" mvn clean install

For windows PowerShell:

$env:demoVar = 'test'; mvn clean install

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM