System Environment Variable Not Found

675 views Asked by At

I'm using MacOS and I have defined a few environment variables inside .bash_profile file. I'm running the Scalatest Framework inside IntelliJ IDEA, and I have defined my test class file:

class ApiCheckerTest extends FunSuite {
    val userid: String = sys.env(Constants.KEY_CLOUD_USERNAME)   //Value of parameter is CLOUD_USERNAME
    val password: String = sys.env(Constants.KEY_CLOUD_PASSWORD)
    val url: String = sys.env(Constants.KEY_CLOUD_DB2_URL)

    test("ApiChecker.getAuthToken.noUserPasswordUrl") {
        val apiChecker = new ApiChecker()
        assert(apiChecker.getAuthToken("", "", "").isEmpty)
    }

    //... <Other test cases>
}

When I run this Test class file, I get an error shown below:

An exception or error caused a run to abort: key not found: CLOUD_USERNAME 
java.util.NoSuchElementException: key not found: CLOUD_USERNAME
    at scala.collection.MapLike$class.default(MapLike.scala:228)
    at scala.collection.AbstractMap.default(Map.scala:59)
    at scala.collection.MapLike$class.apply(MapLike.scala:141)
    at scala.collection.AbstractMap.apply(Map.scala:59)
    at ApiCheckerTest.<init>(ApiCheckerTest.scala:6)     <- points to declaration of val userid
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at org.scalatest.tools.Runner$.genSuiteConfig(Runner.scala:1428)
    at org.scalatest.tools.Runner$$anonfun$31.apply(Runner.scala:1236)
    at org.scalatest.tools.Runner$$anonfun$31.apply(Runner.scala:1235)
    at scala.collection.immutable.List.map(List.scala:284)
    at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1235)
    at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1011)
    at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1010)
    at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1506)
    at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1010)
    at org.scalatest.tools.Runner$.run(Runner.scala:850)
    at org.scalatest.tools.Runner.run(Runner.scala)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:131)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)

Now when I open scala terminal and run sys.env() there, I get an output:

scala> sys.env("CLOUD_USERNAME")
res0: String = sparker0i

If this can work inside Scala terminal, why does this not work in the Test class in IntelliJ?

1

There are 1 answers

0
Sparker0i On

I created these environment variables after I opened IntelliJ IDEA. Restarting the IDE fixed the problems