Ashwin Sreekumar
Ashwin Sreekumar

Reputation: 151

Scala integration test not picking test configuration

I want to write integration test cases in my scala project which uses play framework. I am trying to instantiate a service class with all the dependecies like

class ATest extends AsyncFlatSpec with MockitoSugar{
  implicit val d = org.json4s.DefaultFormats
  implicit val tempEc = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10))
  val logger = CustomLogger.getLogger(this.getClass.getCanonicalName)

  val testConfig = ConfigFactory.load("test.conf")
  val configuration = Configuration(testConfig)
  println(configuration.keys)
  val app1: Application = new GuiceApplicationBuilder().configure(configuration).build
  val service =  Application.instanceCache[AService].apply(app1)

Issue that I am facing is , when I println(configuration.keys) correct keys from test.conf are printed but when val service = Application.instanceCache[DwSqsService].apply(app1) is called it picks application.conf . Why is that ?

I even tried -Dconfig.resource=test.conf flag. Still application.conf is picked. I am not able to find why this behaviour. Any idea ?

Adding more info: have tried adding this into my build.sbt, javaOptions in Test += "-Dconfig.file=conf/test.conf" and javaOptions in Test += "-Dconfig.file=test.conf"

Also have tried using the command line sbt coverage test coverageReport -J-Xmx6g -Dconfig.file=test.conf This works it I bring my application up , but does not in tests

Upvotes: 0

Views: 393

Answers (1)

Felipe Bonezi
Felipe Bonezi

Reputation: 121

You could set a Sbt configuration to load your test.conf file when you're in Test mode.

Test / javaOptions ++= Seq("-Dconfig.resource=test.conf")

When your Play Java project run using sbt test it will load your test.conf that it's into conf folder.

Be aware that the code is to be used on Sbt 1.4+ version. If you're using older versions, you just need to adapt because older version uses other syntax (Maybe javaOptions in Test ++= Seq("-Dconfig.resource=test.conf") but I'm not sure.

Upvotes: 0

Related Questions