Spring has nothing to do with environment variables. It can read them but doesn't say any as far as I know, unlike system properties.
Environment variables are meant to exist in environment.
So you have the following options:
Option 1
Use Power Mock that allows mocking out the static method calls. This library is not a part of spring so you can just use it in a scope of test so that it does no effect on production (powermock jar won't be in the list of dependencies for production)
Option 2
Wrap the static calls with some outer class and then mock it with a regular mocking framework / or load the different bean of the same interface since you're using Spring Test anyway. The code will look something like this:
interface EnvAccessor {
String getValue(String envVarName);
}
public class MyEnvAccessor {
String getValue(String envVarName) {
return System.getenv(envVarName);
}
}
Option 3
Do nothing in the production code but set the env variable programmatically before the JUnit test starts:
public class SampleTest {
@BeforeClass
public static void setupEnvironment() {
// set the environment here
}
}
Now, setting the environment from a test in particular and from java code in general is very tricky, because environment variables shouldn't be changed programmatically.
You can read Here about possible workarounds and evaluate them in your program.