12 factor, Webcompere & JUnit: How to test with env/system variables

Daniel S. Blanco
3 min readJul 26, 2021

--

In a world oriented to the decentralized deployment of our applications in different environments, the methodology to perform these deployments is becoming more and more important. 12 factor is a reference in terms of methodologies to follow. This manifesto indicates 12 steps to follow to improve the deployment of our applications regardless of the language or platform where we are going to deploy them.

We have already talked about this methodology several times in our posts, especially regarding the configuration section. The idea is that it is important that our applications will be configuration agnostic. They are no longer configured depending on the environment in which they are deployed, as we could do with Maven profiles. Instead, the code is always the same, and the package that we generate with this code is independent of the environment in which we are going to deploy it. This avoids problems such as deploying a project with the production configuration in a non-productive environment, with the damage that this would cause.

The way to do this is through the configuration through System or Environment variables. Those values are obtained by the application at runtime. This can lead us to a small problem when performing our unit tests. And it is that we do not have a simple way to set and obtain these.

If we execute the tests through Eclipse, we could set these variables through the execution options enabled by Eclipse itself: Run As > Run Configurations.

This solution may work if we run through Eclipse alone, but it does not work if we run these tests through Maven manually or as part of a pipeline. Therefore it is necessary to find a solution to this problem.

For this, we are going to use the library: system-stubs-core. Which will allow us to emulate these environment variables. And if what we want is to use them in our JUnit 5 tests, we will indicate the system-stubs-jupiter library.

<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>

This emulation can be done in two different ways. But always starting with the inclusion of the ExtendWith annotation and associating it to the SystemStubsExtension class.

On the one hand, we can create a class attribute of type EnvironmentVariables associated with the SystemStub annotation. And then set the environment variables to the instance of the class created associated with the annotation.

A simple example:

But we can also make it even simpler if we pass, as the parameter of the method, the EnvironmentVariables instance.

As with the environment variables, we can do something similar with the system properties. These can also be indicated in Eclipse through JVM variables, or with the WebCompere library and its SystemProperties class.

With this, we have learned about another fantastic library that can help us in the execution of our unit tests.

--

--

No responses yet