§Guiceを使ったテスト
依存性注入にGuiceを使用している場合、テスト用にコンポーネントとアプリケーションの作成方法を直接設定できます。これには、追加のバインディングを追加したり、既存のバインディングをオーバーライドしたりすることが含まれます。
§GuiceApplicationBuilder
GuiceApplicationBuilder は、Application の依存性注入と作成を設定するためのビルダーAPIを提供します。
§インポート
Guiceでアプリケーションを構築するために必要な主なインポートは次のとおりです。
import play.Application;
import play.inject.guice.GuiceApplicationBuilder;
§環境
Environment 、またはアプリケーションのルートパス、モード、クラスローダーなどの環境の一部を指定できます。設定された環境は、アプリケーション設定のロードに使用され、モジュールのロード時に使用され、Playモジュールからバインディングを派生する際に渡され、他のコンポーネントに注入できます。
Application application =
new GuiceApplicationBuilder()
.load(
new play.api.inject.BuiltinModule(),
new play.inject.BuiltInModule(),
new play.api.i18n.I18nModule(),
.in(new Environment(new File("path/to/app"), classLoader, Mode.TEST))
.build();
Application application =
new GuiceApplicationBuilder()
.load(
new play.api.inject.BuiltinModule(),
new play.inject.BuiltInModule(),
new play.api.i18n.I18nModule(),
.in(new File("path/to/app"))
.in(Mode.TEST)
.in(classLoader)
.build();
§設定
追加の設定を追加できます。この設定は、アプリケーション用に自動的にロードされる設定に追加されます。既存のキーが使用されている場合、新しい設定が優先されます。
Config extraConfig = ConfigFactory.parseMap(ImmutableMap.of("a", 1));
Map<String, Object> configMap = ImmutableMap.of("b", 2, "c", "three");
Application application =
new GuiceApplicationBuilder()
.configure(extraConfig)
.configure(configMap)
.configure("key", "value")
.build();
アプリケーション環境からの設定の自動ロードをオーバーライドすることもできます。これはアプリケーション設定を完全に置き換えます。例:
Application application =
new GuiceApplicationBuilder()
.withConfigLoader(env -> ConfigFactory.load(env.classLoader()))
.build();
§バインディングとモジュール
依存性注入に使用されるバインディングは完全に設定可能です。ビルダーメソッドは、Playモジュールとバインディング、およびGuiceモジュールをサポートしています。
§追加のバインディング
Playモジュール、Playバインディング、またはGuiceモジュールを介して追加のバインディングを追加できます。
import static play.inject.Bindings.bind;
Application application =
new GuiceApplicationBuilder()
.bindings(new ComponentModule())
.bindings(bind(Component.class).to(DefaultComponent.class))
.build();
§バインディングのオーバーライド
Playバインディング、またはバインディングを提供するモジュールを使用してバインディングをオーバーライドできます。例:
Application application =
new GuiceApplicationBuilder()
.overrides(bind(Component.class).to(MockComponent.class))
.build();
§モジュールの無効化
ロードされたモジュールは、クラス名で無効にできます。
Application application =
new GuiceApplicationBuilder()
.disable(ComponentModule.class)
.build();
§ロードされたモジュール
モジュールは、play.modules.enabled
設定に基づいてクラスパスから自動的にロードされます。このモジュールのデフォルトのロードはオーバーライドできます。例:
import play.inject.guice.Guiceable;
Application application =
new GuiceApplicationBuilder()
.load(
Guiceable.modules(
new play.api.inject.BuiltinModule(),
new play.api.i18n.I18nModule(),
new play.api.mvc.CookiesModule(),
new play.inject.BuiltInModule()),
Guiceable.bindings(bind(Component.class).to(DefaultComponent.class)))
.build();
§GuiceInjectorBuilder
GuiceInjectorBuilder は、より一般的にGuice依存性注入を設定するためのビルダーAPIを提供します。このビルダーは、GuiceApplicationBuilder
のように環境から設定やモジュールを自動的にロードしませんが、設定とバインディングを追加するための完全にクリーンな状態を提供します。両方のビルダーの共通インターフェースは、GuiceBuilder にあります。Play Injector が作成されます。インジェクタービルダーを使用してコンポーネントをインスタンス化する例を次に示します。
import play.inject.Injector;
import play.inject.guice.GuiceInjectorBuilder;
import static play.inject.Bindings.bind;
Injector injector =
new GuiceInjectorBuilder()
.configure("key", "value")
.bindings(new ComponentModule())
.overrides(bind(Component.class).to(MockComponent.class))
.injector();
Component component = injector.instanceOf(Component.class);
§機能テストでのバインディングのオーバーライド
テストのためにコンポーネントをモックコンポーネントに置き換える例を次に示します。デフォルトの実装とテスト用のモック実装を持つコンポーネントから始めましょう。
public interface Component {
String hello();
}
public class DefaultComponent implements Component {
public String hello() {
return "default";
}
}
public class MockComponent implements Component {
public String hello() {
return "mock";
}
}
このコンポーネントは、モジュールを使用して自動的にロードされます。
import com.google.inject.AbstractModule;
public class ComponentModule extends AbstractModule {
protected void configure() {
bind(Component.class).to(DefaultComponent.class);
}
}
そして、コンポーネントはコントローラーで使用されます。
import play.mvc.*;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class Application extends Controller {
private final Component component;
@Inject
public Application(Component component) {
this.component = component;
}
public Result index() {
return ok(component.hello());
}
}
機能テストで使用する Application
を構築するには、コンポーネントのバインディングをオーバーライドするだけです。
import play.Application;
import play.inject.guice.GuiceApplicationBuilder;
import static play.inject.Bindings.bind;
Application application =
new GuiceApplicationBuilder()
.overrides(bind(Component.class).to(MockComponent.class))
.build();
このアプリケーションは、running
や WithApplication
などのテストヘルパーで使用できます。
このドキュメントに誤りを見つけましたか?このページのソースコードはこちらにあります。ドキュメントガイドラインを読んだ後、プルリクエストを送信してください。質問やアドバイスがありますか?コミュニティフォーラムにアクセスして、コミュニティとの会話を始めてください。