私は手動の依存関係注入のプロジェクトを持っています。標準のPlayテストスイートでアプリケーションをテストすることはできますか?今テストのためにマニュアルDependancy Injection App testing Play 2.5.x
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メカrは依存性を持っています(cacheApi、dbApi ...)
routeメソッドは引数としてアプリケーションを使用できますが、どのようにしてAppLoaderクラスを手動でインスタンス化するためのコンテキストを取得できますか? Scalaの推奨事項で初心者が最も歓迎されています。
https://github.com/playframework/play-scala-compile-di-with-tests
用語コンパイル時依存性注入収率はるか結果、手動依存性の注入を使用した: