I am trying to create a unit test, however, whenever I run it I encounter null values on my variables. Debugging the issue points to my environment variables not being used globally on my unit test. As a class I'm testing calls another class that then references its values from an environment variable. How do I make use of global environment variables in IntelliJ?.
I've already added Environment Variables in the Test Configuration like this,

However, this doesn't seem to be used on the entire project. Hence classes calling this environment variables are returning null. How do I make my Unit Test and the classes it calls to use the globally declared environment variables?. TIA. Below is a snippet of my code of how my environment variables are being used in a class
@Value("${fintech.clientid}")
private String clientId;
@Value("${fintech.secret}")
private String clientSecret;
@Value("${digifi-host}")
private String fintechHost;
@Value("${fintech-digifi-login-endpoint}")
private String loginUrl;
@Value("${fintech-single-session-endpoint}")
private String singleSessionUrl;
What this does is from the Class, it calls the values stored in my application.properties file. And from the application.properties file, it tries to look for the proper values in the environment variables declared on run time.
So from Java Class > application.properties file/config > Runtime Environment Variables
Below is the screenshot of variables with null values when debugging the test. As you can see, all of the values are null which means it didn't load the environment variables I have put in the Unit Test. On the other test case where I had a temporary fix (as I put in the answer here), they are populated and hence loading the environment variables properly, but in my many other test cases like this one, it doesn't.
PS: I've already found related articles in stackoverflow, but they are all test-class specific and that uses surefire plugins or setting the environment variables or via pom, but I don't need it to be there as it is a requirement for us to use environment variables on runtime as the values of the variables should be hidden and not visible on the code. I just simply need the entire project to use a global environment variable when it is doing its Unit Test. Much like how my project would use the environment variables I set in the IDE in normal runtime.
Just for Reference. I already did the ff.:
A.
@ClassRule
public final static EnvironmentVariables environmentVariables = new EnvironmentVariables().set("property1", "value1");
B.
@ContextConfiguration(locations = "classpath:application.properties")
public class LoginServiceTest {
...
}
C.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<SERVER_PORT>8082</SERVER_PORT>
</environmentVariables>
</configuration>
</plugin>
D.
@TestPropertySource(properties = {"property1=value1","property2=value2",})
public class LoginServiceTest {
...
}
E.
public class LoginServiceTest {
static{
System.setProperty("property1", "value1");
System.setProperty("property2", "value2");
...
}


