How to set global environment variables when Unit Testing

iamjoshua :

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.

enter image description here

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

UPDATE:

This solution worked on all of my test. You can refer here for more details.

@RunWith(SpringRunner.class)  //Add this
@SpringBootTest(classes = Application.class)   //Add this
public class LoginServiceTest {
...
}

Then on Run > Edit Configurations > Templates (in sidemenu) > JUnit enter image description here

enter image description here

  1. Then put all your environment variables here.
  2. Click + on the Upper Left window & Apply.
  3. Then delete all existing Unit Test command in your configuration and proceed to re-running all your test again plainly.
  4. Then you'll see the environment variables being added automatically on all your new and upcoming unit test.

    You won't have to manually add an environment variables to each unit test command alright as what I happen to do annoying before. Should you ever have an update on your environment variables. Delete the main unit test configuration you need to have an update, and update the JUnit Template instead so that changes will reflect to new unit tests that you may have.

PS:

Thing is, I still encountered the very same issue on some of my Unit Test, that even though the environment variables are being read properly on runtime and in some unit test alright with the above configurations. It is only during the other many Unit Test the very same environment variables are not being retrieved or received properly by the classes. At first I thought my classes are not really getting the proper environment variables and there must be something wrong as to how I stored the environment variables in IntelliJ, which it did not really, but it turns out this code format I have is the issue.

@Value("${fintech.clientid}")
private String clientId;
@Value("${fintech.secret}")
private String clientSecret;
@Value("${digifi-host}")
private String digifiHost;

It turns out,even though though this particular code works on runtime, it wont on Unit Test time, like ever.

Hence I tinkered with this particular codes until I was already able to get the proper environment variables on Unit Test. Refer to the changes from the code above to the one that fixed the issue for me below.

@Value("${fintech.clientid}")
public void getClientId(String tempClientId) {
    clientId = tempClientId;
}
private  String clientId;

@Value("${fintech.secret}")
public void getClientSecret(String tempClientSecret) {
    clientSecret=tempClientSecret;
}
private  String clientSecret;

@Value("${digifi-host}")
public void getDigifiHost(String tempDigifiHost) {
    digifiHost=tempDigifiHost;
}
private  String digifiHost;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=401421&siteId=1