I´m trying to add a new environment variable in my test but I see that
sys.env
is an immutable map, so I cannot see a way to add a new env var.
Any idea how?
I´m trying to add a new environment variable in my test but I see that
sys.env
is an immutable map, so I cannot see a way to add a new env var.
Any idea how?
The code you are testing should not depend on sys.env
directly. Make environment: Map[String, String]
a parameter - then you can pass any data you want in for testing, while still using sys.env
in production.
Although it's kind of a different question, I think this answer applies: You can't modify the current process' environment (i.e. it's read-only).
As Dima said, you can use a map to save the properties or, like the answer I mentioned suggests, use scala.util.Properties, which gives you access to the current env and allows you to set your own properties.
If using Properties is definitely not an option, and you really need to set some env vars for all the tests, maybe you can use SBT to do that, like this:
fork in Test := true
envVars in Test := Map("ENV_VAR" -> "value")
The fork
part is essential. Otherwise, the tests will run in the current process and won't pick up the new env vars.