【发布时间】: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