在我的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?相关
发布于 2021-09-16 23:05:05
TypeLiteral在Play Guice API的scala实现中还不可用。
泛型的当前有效解决方案是使用所需的模拟定义创建一个测试模块,并将其传递到overrides中
object CustomMockComponentModule extends AbstractModule {
val agRepoMock = ...
@Provides
@Singleton
def mockBean(): AccessGroupRepository[IO] = agRepoMock
}
...
override val application: Application = guiceApplicationBuilder
.overrides(CustomMockComponentModule)
.build() https://stackoverflow.com/questions/56691398
复制相似问题