测试时如何在Scala Guice中覆盖TypeLiteral

时间:2019-06-20 17:48:49

标签: scala playframework guice

在我的Module.scala中,我绑定了如下特质的具体实现:

trait AccessGroupRepository[F[_]] {}

@Singleton
class AccessGroupRepositoryImpl @Inject()(db: OldDataBase, c: IOContextShift)
    extends AccessGroupRepository[IO] {}

并使用TypeLiteral完成绑定:

bind(new TypeLiteral[AccessGroupRepository[IO]] {}).to(classOf[AccessGroupRepositoryImpl])

现在,在使用Mockito模拟进行测试时,我需要覆盖此绑定:

override val application: Application = guiceApplicationBuilder
    .overrides(bind(new TypeLiteral[AccessGroupRepository[IO]] {}).to(agRepoMock))

但出现以下错误:

overloaded method value bind with alternatives:
[error]   [T](implicit evidence$1: scala.reflect.ClassTag[T])play.api.inject.BindingKey[T] <and>
[error]   [T](clazz: Class[T])play.api.inject.BindingKey[T]
[error]  cannot be applied to (com.google.inject.TypeLiteral[api.v1.accessgroup.AccessGroupRepository[cats.effect.IO]])
[error]     .overrides(bind(repoTypeLiteral).to(agRepoMock))
[error]                ^

我该如何解决?

此问题与How to bind a class that extends a Trait with a monadic type parameter using Scala Guice?

有关

0 个答案:

没有答案