Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

junit5

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.

like image 616
ashish choudhary Avatar asked Jul 04 '19 12:07

ashish choudhary


People also ask

How do I set environment variables in JUnit tests?

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 (); }

What is @parameterizedtest In JUnit?

Parameterized tests are like other tests except that we add the @ParameterizedTest annotation: JUnit 5 test runner executes this above test — and consequently, the isOdd method — six times. And each time, it assigns a different value from the @ValueSource array to the number method parameter.

How to install JUnit in Windows 10?

Step 1: Verify Java Installation in Your Machine Step 2: Set JAVA Environment Step 3: Download JUnit Archive Step 4: Set JUnit Environment Step 5: Set CLASSPATH Variable Step 6: Test JUnit Setup Step 7: Verify the Result

How does the isodd method work in JUnit 5?

JUnit 5 test runner executes this above test — and consequently, the isOdd method — six times. And each time, it assigns a different value from the @ValueSource array to the number method parameter. So, this example shows us two things we need for a parameterized test:


2 Answers

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();
}
like image 133
Miguel Ferreira Avatar answered Sep 18 '22 13:09

Miguel Ferreira


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"));
    }

}
like image 41
wumpz Avatar answered Sep 19 '22 13:09

wumpz