mercredi 2 septembre 2015

How to override guice modules in Playframework unit tests using ScalaTest

I want to write functional test for my controller in PlayFramework. To do that I want to mock implementation of some classes.

I found nice example of how to do that using spec2 here: http://ift.tt/1Oaj9a1

But I'm using scala test with OneAppPerSuite trait that uses FakeApplication. Here are documentation: http://ift.tt/1JNi9J6

Problem is that i cannot found a way to intercept into GuiceApplicationBuilder and override some bindings with mock implementation.

Here are FakeApplication implementation from play.api.test:

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

So there is no way for me to intercept into GuiceApplicationBuilder and override bindings.

I'm new to playframework so sorry if question looks a bit silly. Thanks!

Aucun commentaire:

Enregistrer un commentaire