Documentation

§Guice を使用したテスト

Guice を 依存性注入 に使用している場合は、コンポーネントやアプリケーションがテストのためにどのように生成されるかを直接設定することができます。これはバインディングを追加したり、既存のバインディングをオーバーライドすることも含みます。

§GuiceApplicationBuilder

GuiceApplicationBuilderApplication の生成と依存性注入の設定のためのビルダー API を提供します。

§Environment

Environment 、すなわちアプリケーションのルートパスやモード、クラスローダなどの環境のパーツを定義できます。設定された環境はアプリケーションの設定をロードするのに使用されます (アプリケーションの設定はモジュールをロードする際に使用され、Play のモジュールからバインディングを引き出す際に使用されます。また、他のコンポーネントに注入することもできます)。

import play.api.inject.guice.GuiceApplicationBuilder
val application = new GuiceApplicationBuilder()
  .in(Environment(new File("path/to/app"), classLoader, Mode.Test))
  .build
val application = new GuiceApplicationBuilder()
  .in(new File("path/to/app"))
  .in(Mode.Test)
  .in(classLoader)
  .build

§設定

設定を追加することもできます。この設定は、アプリケーションに自動的にロードされる設定に加えてつねに有効です。既にキーが使用されている場合は新しい設定が優先されます。

val application = new GuiceApplicationBuilder()
  .configure(Configuration("a" -> 1))
  .configure(Map("b" -> 2, "c" -> "three"))
  .configure("d" -> 4, "e" -> "five")
  .build

設定のアプリケーションの環境からの自動ロードは、上書きされる可能性があります。これはアプリケーションの設定に完全に置き換えることができます。

val application = new GuiceApplicationBuilder()
  .loadConfig(env => Configuration.load(env))
  .build

§バインディングとモジュール

依存性注入に使用されるバインディングは完全に設定可能です。ビルダーメソッドは Play のモジュールとバインディング あるいは Guice のモジュールをサポートしています。

§追加のバインディング

Play のモジュール、Play のバインディングあるいは Guice のモジュールから、バインディングを追加することができます。

import play.api.inject.bind
val injector = new GuiceApplicationBuilder()
  .bindings(new ComponentModule)
  .bindings(bind[Component].to[DefaultComponent])
  .injector

§バインディングのオーバーライド

Play のバインディングあるいはバインディングを提供するモジュールを使って、バインディングをオーバーライドすることもできます。

val application = new GuiceApplicationBuilder()
  .overrides(bind[Component].to[MockComponent])
  .build

§モジュールを無効化する

いかなるモジュールもクラス名を指定して無効化できます。

val injector = new GuiceApplicationBuilder()
  .disable[ComponentModule]
  .injector

§ロードされたモジュール

モジュールは play.modules.enabled 設定に従い、クラスパスから自動的にロードされます。このようなモジュールのデフォルトのロードは上書きできます。

val injector = new GuiceApplicationBuilder()
  .load(
    new play.api.inject.BuiltinModule,
    bind[Component].to[DefaultComponent]
  ).injector

§GuiceInjectorBuilder

GuiceInjectorBuilder は、Guice の依存性注入をより一般的に設定するビルダー API を提供します。このビルダーは GuiceApplicationBuilder のように自動的には環境から設定やモジュールをロードしませんが、設定やバインディングを追加するのに完全にクリーンな状態を提供します。それぞれのビルダーに共通のインターフェースは GuiceBuilder にあります。Play の Injector が生成されます。以下は injector ビルダーを使用したコンポーネントをインスタンス化する例です。

import play.api.inject.guice.GuiceInjectorBuilder
import play.api.inject.bind
val injector = new GuiceInjectorBuilder()
  .configure("key" -> "value")
  .bindings(new ComponentModule)
  .overrides(bind[Component].to[MockComponent])
  .injector

val component = injector.instanceOf[Component]

§機能テストにおけるバインディングのオーバーライド

これはテスト用のモックコンポーネントで置き換える例です。標準の実装を持ち、テスト用にはモック実装を持つコンポーネントを使ってみましょう。

trait Component {
  def hello: String
}

class DefaultComponent extends Component {
  def hello = "default"
}

class MockComponent extends Component {
  def hello = "mock"
}

コンポーネントはモジュールを利用して自動的にロードされます。

import play.api.{ Environment, Configuration }
import play.api.inject.Module

class ComponentModule extends Module {
  def bindings(env: Environment, conf: Configuration) = Seq(
    bind[Component].to[DefaultComponent]
  )
}

そしてコンポーネントはコントローラに利用されます。

import play.api.mvc._
import javax.inject.Inject

class Application @Inject() (component: Component) extends Controller {
  def index() = Action {
    Ok(component.hello)
  }
}

Application をビルドして機能テストの中で使用するためには、コンポーネントのバインディングを単純にオーバーライドすることができます。

import play.api.inject.guice.GuiceApplicationBuilder
import play.api.inject.bind
val application = new GuiceApplicationBuilder()
  .overrides(bind[Component].to[MockComponent])
  .build

生成されたアプリケーションは Specs2ScalaTest のための機能テストヘルパーとともに使用することができます。

Next: データベースを使ったテスト


このドキュメントの翻訳は Play チームによってメンテナンスされているものではありません。 間違いを見つけた場合、このページのソースコードを ここ で確認することができます。 ドキュメントガイドライン を読んで、お気軽にプルリクエストを送ってください。