1

In my Module.scala I'm binding a concrete implementation of a trait defined as follows:

trait AccessGroupRepository[F[_]] {}

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

and the binding is done using TypeLiteral:

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

Now, I need to override this binding when testing with a Mockito mock:

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

but I get the following error:

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 could I solve that?

This question relates to How to bind a class that extends a Trait with a monadic type parameter using Scala Guice?

sentenza
  • 1,608
  • 3
  • 29
  • 51
  • Do you have a test module? Can you show it? Think that this question and the marked answer below can help https://stackoverflow.com/questions/483087/overriding-binding-in-guice/27803015 – Haim Raman Jun 21 '19 at 06:09
  • Unfortunately no, I haven't a test module, but essentially I need to find a way to do an `.overrides(bind(new TypeLiteral[REPO[IO]] {}).to(MOCK))` – sentenza Jun 21 '19 at 07:48
  • I not familiar with Play and scale, but you need to put the override in the test moudle and do the scale equivalent to Guice.createInjector(Modules.override(new ProductionModule()).with(new TestModule())); – Haim Raman Jun 21 '19 at 08:56

1 Answers1

1

TypeLiteral isn't available yet in scala implementation of Play Guice API.

Current valid solution for generics, is creating a test module with desired mock definitions, and passing it within overrides:

object CustomMockComponentModule extends AbstractModule {
  val agRepoMock = ...

  @Provides
  @Singleton
  def mockBean(): AccessGroupRepository[IO] = agRepoMock
}

...

override val application: Application = guiceApplicationBuilder
    .overrides(CustomMockComponentModule)
    .build()   

veysiertekin
  • 1,731
  • 2
  • 15
  • 40