【问题标题】:Reading file from conf with Scala and Play! 2.5使用 Scala 和 Play 从 conf 中读取文件! 2.5
【发布时间】:2020-05-27 19:12:25
【问题描述】:

“file.csv”在 /conf 文件夹中定义,我正在尝试使用 Scala 工作表测试读取文件:

import javax.inject._
import play.api.Environment
import play.api.mvc.{AbstractController, ControllerComponents}

class CountController @Inject() (cc: ControllerComponents,
                                 env: Environment) extends AbstractController(cc) {

   def getter() = Option(env.classLoader.getResourceAsStream("file.csv"))

}

new CountController().getter();

我是否需要模拟 ControllerComponentsEnvironment 才能访问该文件? :

new CountController(mock ControllerComponents , mock )Environment.getter();

阅读https://www.playframework.com/documentation/2.8.x/ScalaTestingWithGuice 并没有详细说明模拟环境。我',

【问题讨论】:

  • 为什么要模拟环境?直接构造它:Environment.simple()。顺便说一句,如果你想检索文件,有一种更简单的方法:env.resourceAsStream("file.csv")

标签: scala playframework


【解决方案1】:

根据@cbley 评论,使用Environment.simple()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-19
    • 2018-10-18
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多