【问题标题】:Manual Dependancy Injection App testing Play 2.5.x手动依赖注入应用程序测试 Play 2.5.x
【发布时间】:2016-10-14 16:06:43
【问题描述】:

我有手动依赖注入的项目。我可以使用标准 Play 测试套件测试我的应用程序吗?

play.application.loader="AppLoader"

    class AppLoader extends ApplicationLoader  {
  override def load(context: Context): Application = {
    LoggerConfigurator(context.environment.classLoader).foreach(_.configure(context.environment))
    new AppComponents(context).application
  }
}
}

class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with EhCacheComponents with EvolutionsComponents with DBComponents with HikariCPComponents{

  lazy val applicationController = new controllers.Application(defaultCacheApi, dbApi.database("default"))
  lazy val usersController = new controllers.Users(defaultCacheApi)
  lazy val assets = new controllers.Assets(httpErrorHandler)

  //applicationEvolutions

  // Routes is a generated class
  override def router: Router = new Routes(httpErrorHandler, applicationController, usersController, assets)

现在测试很简单

class ApplicationTest extends PlaySpec with OneAppPerTest {

 "Application" must {

"send 404 on a bad request" in {
  route(FakeRequest(GET, "/boum")) mustBe None
}
}
}

测试以错误结束:

Could not find a suitable constructor in controllers.Application. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument 

我想我需要以某种方式使用我的 AppLoader 而不是 ApplicationTest 类中的默认 Guice 机制,因为应用程序控制器具有依赖性(cacheApi,dbApi ...)

route 方法可以将应用程序作为参数,但是如何获取上下文以手动实例化 AppLoader 类? Scala 建议中的新手是最受欢迎的。

【问题讨论】:

    标签: scala testing dependency-injection scalatest playframework-2.5


    【解决方案1】:

    这个例子回答了我所有的问题:

    https://github.com/playframework/play-scala-compile-di-with-tests

    使用术语编译时依赖注入产生比手动依赖注入更多的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多