"Live" Environment Variables in @SpringBootTest RANDOM_PORT app aren't found
Closed this issue · 3 comments
If you have a @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
like the provided example, environment variables in the servlet container won't get picked up after the ApplicationContext is initialized. In fact, System.getenv
will get null
even for values that were available during startup and that are still "live" in the test case.
If tests are @SpringBootTest
or @WebMvcTest
then the default is MockMvc environment and a servlet container isn't started up. This allows the environment variables to affect the mock environment, even after the ApplicationContext is initialized.
I imagine that the values are being provided initially and then look no further because the servlet container is in a completely different process.
I can understand the limitation but wasn't clear to me without some testing. Perhaps some more documentation would help. I created a repo with some of my testing and found that SpringBootTestRandomPortTest.springBootTest
fails on this line because the environment variable value is neither the original, nor the new...but null
.
Feel free to close this issue but leaving it here for others that may find it helpful.
Great project. Thanks!
MockMvc has a different lifecycle as you say. If you wish to add some documentation as a PR, then please do. Perhaps a child document on Environment Variables and Spring Tests
we can link from the main README. Feel free to add your example tests to the Spring examples too.
@sdoxsee ^^^
Going to close this for now but may come back with a PR some other time. Thanks!