While writing tests, some of the target code behaves differently depending on the value of a certain environment variable. In order to test its functionality, I need to change the variable and execute the code being tested. Unfortunately, there seems to be no sensible way to do it from within our Spek tests. The map that System.getenv()
gives me is read-only.
Asked
Active
Viewed 30 times
0

rbanffy
- 2,319
- 1
- 16
- 27
-
See answer beginning ["*For use in scenarios where you need to set specific environment values for unit tests*"](https://stackoverflow.com/a/7201825/1898563) – Michael Jan 02 '20 at 15:58
-
Not sure how this solves the issue, @Michael. It's a) a horrid hack and, b) not written in Kotlin. – rbanffy Jan 02 '20 at 16:43
-
Java methods are interoperable with kotlin – Michael Jan 02 '20 at 16:58
-
Also, using `org.junit.contrib.java.lang.system.EnvironmentVariables` it's possible to sidestep the issue (without the hack), but Spek doesn't currently pick it (or at least my corporate endorsed release doesn't). – rbanffy Jan 02 '20 at 16:58