【问题标题】:Scala/Play: Test class: reading a custom configuration file from the /conf folderScala/Play:测试类:从 /conf 文件夹读取自定义配置文件
【发布时间】:2020-09-19 07:26:33
【问题描述】:

使用Play 2.7。在一个测试类中,我想测试一个读取配置(属性)文件conf/fun.conf的服务。

我找不到任何如何执行此壮举的示例。我查看了 Play 文档以及此处的问题,但一无所获。

我写的服务通过注入得到Configuration

class MyFunService @Inject() (config: Configuration) extends FunService {

    override def getValue(key: String) = config.get[String](key)

}

我想通过测试调用此服务:

class FunSpec(implicit ee: ExecutionEnv) extends Specification {

  sequential

  "The FunService" should {

    "retrieve a value" in new WithApplication() {

      //Oops! Does not actually read the conf file...
      val env = play.api.Environment.simple()
      val config = play.api.Configuration.load(env)

      //...so getting an Exception here.
      val f = Future(new MyFunService(config).getValue("fun_stuff"))

      f must beEqualTo("having_fun").await(retries = 0, timeout = 5.seconds)
    }
  }
}

【问题讨论】:

    标签: scala playframework specs2


    【解决方案1】:

    我正在使用这段代码来读取测试资源

    import com.google.common.base.Charsets.UTF_8
    import com.google.common.io.Resources
    
    object ResourceFileSupport {
      def read(file: String) =
        Resources.toString(
          Option( this.getClass.getClassLoader.getResource(file) ).getOrElse {
            throw new IllegalArgumentException(s"resource $file not found in classpath.")
          }, UTF_8)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 2017-01-27
      相关资源
      最近更新 更多