0

I have project with manual dependency injection. Can I test my application with standard Play test suite?

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)

For now test is very simple

class ApplicationTest extends PlaySpec with OneAppPerTest {

 "Application" must {

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

Test ends up with error:

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 

I presume I need somehow use my AppLoader instead of defualt Guice mechanisam inside ApplicationTest class, because Application controller has dependacy ( cacheApi, dbApi ...)

route method can take application as argument but how can I obtain context to manually instantiate AppLoader class ? Being newbie in Scala recommendations are most welcomed.

Levijatanu
  • 371
  • 2
  • 17

1 Answers1

0

This example answered all of my questions:

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

Using term compile time dependency injection yield much more results then manual dependency injection.

Levijatanu
  • 371
  • 2
  • 17