Search code examples
scalascala-catsmockito-scala

Scala Cats spy a method with mockito-scala-cats


I am trying to mock a method with mockito-scala-cats

For example

this is my class

class MyService {

  def getProperty(property: String): Either[Future, String, ExternalUser] = ???

}

and the test class

class MyServiceSpec extends FunSpec with MockitoSugar with MockitoCats {

  describe("MyServiceApiImpl") {
    it("get property") {
      val serviceApi = mock[MyService]
      whenF(serviceApi.getProperty("name")) thenReturn UserExternal()
    }
  }

}

I get

could not find implicit value for parameter a: cats.Applicative[[B]cats.data.EitherT[scala.concurrent.Future,String,B]]


Solution

  • Check your imports. The following code compiles for me

    import org.mockito.MockitoSugar
    import org.mockito.cats.MockitoCats
    import org.scalatest.FunSpec
    
    import scala.concurrent.Future
    import scala.concurrent.ExecutionContext.Implicits.global // check this import
    
    import cats.data.EitherT
    import cats.instances.future._ // and this import
    
    class MyService {
    
      def getProperty(property: String): EitherT[Future, String, ExternalUser] = ???
    
    }
    
    class MyServiceSpec extends FunSpec with MockitoSugar with MockitoCats {
    
      describe("MyServiceApiImpl") {
        it("get property") {
          val serviceApi = mock[MyService]
          whenF(serviceApi.getProperty("name")) thenReturn ExternalUser()
        }
      }
    
    }
    
    case class ExternalUser()