2017-06-23 13 views
0

現在、私のアプリをPlay 2.4からPlay 2.5に移行中です。これまでのところ、大変な苦労をしています。Play 2.5コンパイル時のGuiceエラー

今、私はテストを合格させようとしています。

一部と1つのコントローラは、テスト側からのparams

class UserLogin @Inject() (
    loginService: UserLoginService, 
    authConfig: AuthConfig, 
    oAuthConfig: OAuthConfig, 
    env: PureEnvironment, 
    //stringResources: StringResources, 
    messagesApi: MessagesApi 
) extends BaseController(messagesApi: MessagesApi) { 

    //endpoints 
} 

を注入された私は、私は彼らが奇妙なことは、私は非常に類似していることをあるGuiceの

object IdentityManagerModuleMock extends AbstractModule with ScalaModule { 
    def configure: Unit = { 
    bind[PureEnvironment].toInstance(MockEnvironment) 
    val conf = Configuration.reference 
    val messagesApi = new DefaultMessagesApi(Environment.simple(), conf, new DefaultLangs(conf)) 
    bind[MessagesApi].toInstance(messagesApi) 

    bind[AuthConfig].toInstance(
     AuthConfig(
     "https://localhost", 
     30, 
     3600, 
     86400, 
     Seq(InetAddress.getByName("127.0.0.1")), 
     Seq(InetAddress.getByName("127.0.0.1")), 
     "https://localhost/login" 
    ) 
    ) 
    bind[OAuthConfig].toInstance(oAuthConfigMock) 

    bind[UserLoginService].to[UserLoginServiceImpl] 
} 

val injector = Guice.createInjector(IdentityManagerModuleMock) 

When I enable the routes in my routes file, 
POST /login com.dummy.im.controllers.UserLogin.login 
POST /reset com.dummy.im.controllers.UserLogin.reset 

1) No implementation for com.dummy.im.components.UserLoginService was bound. 
[info] while locating com.dummy.im.components.UserLoginService 
[info]  for parameter 0 at com.dummy.im.controllers.UserLogin.<init>(UserLogin.scala:24) 
2) No implementation for com.dummy.platform.PureEnvironment was bound. 
[info] while locating com.dummy.platform.PureEnvironment 
[info]  for parameter 3 at com.dummy.im.controllers.UserLogin.<init>(UserLogin.scala:24) 
3) No implementation for scala.collection.Seq<java.net.InetAddress> was bound. 
[info] while locating scala.collection.Seq<java.net.InetAddress> 
[info]  for parameter 4 at com.dummy.im.config.AuthConfig.<init>(AuthConfig.scala:13) 

5) Could not find a suitable constructor in java.lang.Integer. Classes must have either one 
(and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. 
[info] at java.lang.Integer.class(Integer.java:52) 
[info] while locating java.lang.Integer 
[info]  for parameter 1 at com.dummy.im.config.AuthConfig.<init>(AuthConfig.scala:13) 

を注入することが規定されていている

(実際に私が知っている限り、Playによって注入された)MessagesAPIと、.confファイルから読み込まれた設定オブジェクトを使わずに、アプリケーションを実際に実行するためのconfigです。

object IdentityManagerModule extends AbstractModule with ScalaModule { 

    def configure: Unit = { 
    bind[PureEnvironment].to[PlayProductionEnvironmentImpl] 

    bind[OauthRepository].to[OauthRepositoryImpl] 

    bind[UserLoginService].to[UserLoginServiceImpl] 
    } 
} 

これはうまく動作します。

私が変更した主なものは、依存関係をコントローラのMessagesAPIに追加することでした。

なぜGuiceがIdentityManagerModuleMockでバインドされたものを見ることができないのか分かりません。

すべてのアイデアが歓迎されています。私は過去数日間思っていたことをすべて読んで試しました。

EDIT: 我々は.confファイルでカスタムアプリケーションローダー

class CustomApplicationLoader extends GuiceApplicationLoader() { 
    //config validation 

    override def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = { 
    val conf = context.initialConfiguration 
    initialBuilder 
     .in(context.environment) 
     .loadConfig(conf) 
     .overrides(overrides(context): _*) 
     .bindings(
     MiscModule, 
     CommonConfigurationModule, 
     IdentityManagerConfigModule, 
     IdentityManagerModule, 
     ApplicationLifecycleModule 
    ) 
    } 
} 

を持っている、あなたがのためにカスタムアプリケーションローダを必要とするようにそれが見えない

play.application.loader = "com.dummy.guice.CustomApplicationLoader" 

答えて

1

として使用しているものあなたはやっている。すぐに使用できるMessageApiを無効にしてからapplication.confで独自のモジュールを追加すると、MessagesApiを上書きする必要はありません。

https://www.playframework.com/documentation/2.5.x/ScalaPlayModules#Registration-Overrides

あなたはGuiceのを含むテストを実行している場合は、WithApplicationを使用することができます。

https://www.playframework.com/documentation/2.5.x/ScalaFunctionalTestingWithSpecs2#WithApplication

WithApplicationがGuiceApplicationBuilderを呼び出しますので、あなたはこれまで、直接Guice.createInjectorを呼び出す必要はありません。

https://www.playframework.com/documentation/2.5.x/ScalaTestingWithGuice#GuiceApplicationBuilder

サンプルプロジェクトをチェックアウト:それらはすべて「2.5.xのを持っています「枝とほとんどがそれらに統合テストを持っていると、ガイドとしてそれらを活用:

https://github.com/playframework?utf8=%E2%9C%93&q=examples&type=&language=

関連する問題