ドキュメント

§Guiceを使ったテスト

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

§GuiceApplicationBuilder

GuiceApplicationBuilderは、依存性注入とApplicationの作成を設定するためのビルダーAPIを提供します。

§環境

Environment、またはルートパス、モード、アプリケーションのクラスローダーなどの環境の一部を指定できます。設定された環境は、アプリケーション設定の読み込みに使用され、モジュールの読み込み時やPlayモジュールからのバインディングの導出時に使用され、他のコンポーネントへの注入も可能です。

import play.api.inject.guice.GuiceApplicationBuilder
val application = new GuiceApplicationBuilder()
  .load(
    new play.api.inject.BuiltinModule,
    new play.api.i18n.I18nModule,
    new play.api.mvc.CookiesModule
  .in(Environment(new File("path/to/app"), classLoader, Mode.Test))
  .build()
val application = new GuiceApplicationBuilder()
  .load(
    new play.api.inject.BuiltinModule,
    new play.api.i18n.I18nModule,
    new play.api.mvc.CookiesModule
  .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,
    new play.api.i18n.I18nModule,
    new play.api.mvc.CookiesModule,
    bind[Component].to[DefaultComponent]
  )
  .injector()

§GuiceInjectorBuilder

GuiceInjectorBuilderは、Guice依存性注入をより一般的に設定するためのビルダーAPIを提供します。このビルダーは、`GuiceApplicationBuilder`のように環境から設定やモジュールを自動的にロードしませんが、設定とバインディングを追加するための完全にクリーンな状態を提供します。両方のビルダーの共通インターフェースはGuiceBuilderにあります。Play 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.inject.Binding
import play.api.inject.Module
import play.api.Configuration
import play.api.Environment

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

そして、コンポーネントはコントローラーで使用されます。

import javax.inject.Inject

import play.api.mvc._

class Application @Inject() (component: Component, cc: ControllerComponents) extends AbstractController(cc) {
  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の機能テストヘルパーで使用できます。

次へ: コンパイル時依存性注入を使ったテスト


このドキュメントにエラーを発見しましたか?このページのソースコードはこちらにあります。ドキュメントガイドラインを読んだ後、プルリクエストを送信してください。質問やアドバイスを共有したいですか?コミュニティフォーラムにアクセスして、コミュニティとの会話を始めましょう。