# Configure Environment Variables in JUnit Tests

> In this article, we'll write a JUnit Test configured with Environment Variables using `junit-pioneer` library.

First of all, we need to add the `junit-pioneer` library and `maven-surefire-plugin` to the Maven pom.xml.

```xml
<project>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <build>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.5.3</version>
                <configuration>
                    <includes>
                        <include>**/*Test</include>
                    </includes>
                    <failIfNoTests>true</failIfNoTests>
                    <useFile>false</useFile>
                    <!--
                        Make it able to modify environment variables for tests.
                        Ref:  https://junit-pioneer.org/docs/environment-variables/#warnings-for-reflective-access
                    -->
                    <argLine>
                        --add-opens java.base/java.util=ALL-UNNAMED
                        --add-opens java.base/java.lang=ALL-UNNAMED
                    </argLine>
                </configuration>
            </plugin>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.11.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit-pioneer</groupId>
            <artifactId>junit-pioneer</artifactId>
            <version>2.3.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
```

Then, we can write unit test configured with environment variables.

```java
  @Test
  @SetEnvironmentVariable(key = "CI_ENVIRONMENT_NAME", value = "test-dev")
  @SetEnvironmentVariable(key = "AWS_ACCOUNT_ID", value = "some-account-id")
  @SetEnvironmentVariable(key = "VPC_ID", value = "some-vpc-id")
  @SetEnvironmentVariable(key = "VPC_NAME", value = "some-vpc-name")
  void testAppWithEnvSet() {
    assertDoesNotThrow(() -> AwsApiGwCdkApp.main(null));
  }

  @Test
  void testAppWithoutEnvSet() {
    assertThrows(IllegalArgumentException.class, () -> AwsApiGwCdkApp.main(null));
  }
```

Reference GitLab repo: https://gitlab.com/ada-public-projects/aws/aws-api-gateway-using-cdk
