从单元测试中读取配置文件

问题描述

我有一个非常简单的项目。

build.sbt:

Z

资源文件夹下的两个配置文件:

应用程序.conf

scalaVersion := "2.13.5"

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

libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "3.2.3" % Test,"com.typesafe" % "config" % "1.4.1"
)

test.conf

some-value = "value from application.conf file"

并且只有 1 个规范测试类:

SomeTestSpec:

some-value = "value from test.conf file"

当我运行测试失败时:

class SomeTestSpec extends AnyFlatSpec with Matchers {
  val config: Config = ConfigFactory.load()

  "some test" should "read and print from test.conf" in {
    val value = config.getString("some-value")
    println(s"value of some-value = $value")

    value shouldBe "value from test.conf file"
  }
}

为什么规范读取文件 "value from [application].conf file" was not equal to "value from [test].conf file" ScalaTestFailureLocation: SomeTestSpec at (SomeTestSpec.scala:12) Expected :"value from [test].conf file" Actual :"value from [application].conf file" 而不是 application.conf? build.sbt 有问题吗?:

test.conf

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)