Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking a method which returns an fs2.Stream

Tags:

mockito

scala

fs2

Why isn't it possible to mock a method that returns an fs2.Stream with a Mockito mock?

Mockito is complaining that I am trying to return a FreeC instead of a Stream. Why is that and how can I get it working?

The following code:

import cats.effect.IO
import fs2.Stream
import org.mockito.Mockito.when
import org.scalatest.FlatSpec
import org.scalatest.mockito.MockitoSugar

trait MyTrait {
  def method: Stream[IO, Int]
}

class TestSpec extends FlatSpec with MockitoSugar {

  val m: MyTrait = mock[MyTrait]
  val emptyReturn: Stream[IO, Int] = Stream.emits(List.empty[Int])

  when(m.method).thenReturn(emptyReturn)

  ...
}

gives this error:

TestSpec:
TestSpec *** ABORTED ***
  org.mockito.exceptions.misusing.WrongTypeOfReturnValue: Stream cannot be returned by method()
method() should return FreeC
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
  at TestSpec.<init>(TestSpec.scala:15)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  at java.lang.Class.newInstance(Class.java:442)
  at org.scalatest.tools.Framework$ScalaTestTask.execute(Framework.scala:435)
  at sbt.TestRunner.runTest$1(TestFramework.scala:76)
  at sbt.TestRunner.run(TestFramework.scala:85)
  at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:202)
  ...
like image 964
Johan J Avatar asked Mar 02 '18 14:03

Johan J


1 Answers

I'm not sure about the mockito side, but there's no need to use mocking at all;

val m: MyTrait = new MyTrait {
  def method: Stream[IO, Int] = Stream.emits(List.empty[Int]).covary[IO]
}
like image 75
Daenyth Avatar answered Sep 20 '22 23:09

Daenyth