Scala Testing application integration with guice context

I am new to framework development and have limited experience with scala. Using play framework 2.4 , I am trying to write full integration tests in which I want to call a controller and use in-memory db to get data from it. I use scalatest-plus with scalatest . I read about testing game modules in the game documentation and am now trying to use play.api.test.FakeApplication , but I cannot find a way to inject my guice module into this fake application. I am using the OneAppPerSuite trait which I can override FakeApplication but cannot pass any guice module for injection. Here is the source of FakeApplication from:

 case class FakeApplication( override val path: java.io.File = new java.io.File("."), override val classloader: ClassLoader = classOf[FakeApplication].getClassLoader, additionalPlugins: Seq[String] = Nil, withoutPlugins: Seq[String] = Nil, additionalConfiguration: Map[String, _ <: Any] = Map.empty, withGlobal: Option[play.api.GlobalSettings] = None, withRoutes: PartialFunction[(String, String), Handler] = PartialFunction.empty) extends Application { private val app: Application = new GuiceApplicationBuilder() .in(Environment(path, classloader, Mode.Test)) .global(withGlobal.orNull) .configure(additionalConfiguration) .bindings( bind[FakePluginsConfig] to FakePluginsConfig(additionalPlugins, withoutPlugins), bind[FakeRouterConfig] to FakeRouterConfig(withRoutes)) .overrides( bind[Plugins].toProvider[FakePluginsProvider], bind[Router].toProvider[FakeRouterProvider]) .build .... 

Thus, I cannot pass the user module here.

This is what my test looks like:

 class UsersControllerTest extends PlaySpec with OneAppPerSuite { override implicit lazy val app = FakeApplication(additionalConfiguration = inMemoryDatabase()) "Application " should { "work" in { val resp = route(FakeRequest(GET, "/api/users")).get } } 

}

And here is my controller that I want to check:

 class UserController @Inject() (userService: UserService) (implicit ec: ExecutionContext) extends Controller { def get = Action.async { implicit request => { // implementation } } 

}

Obviously, my test now fails because it cannot find an instance of UserService . So my question is:

  • How to write end2end integration tests with a game using guice context
  • Is it normal to write such tests in a game application, since I cannot find any worthy examples or documentation for doing something like this. I used such tests in Java applications, where integration tests load the spring context, but cannot find such examples in the game.

Thanks in advance.

+6
source share
1 answer
+2
source

All Articles