1

I am facing to mock nested function problem in Scala context. I have found answer like this: How to mock fields of inner singleton objects? which works but isn't concise. I am wondering in Scala community is there something like mockito's deepstub?

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
RioAraki
  • 534
  • 2
  • 13

1 Answers1

3

You can use ReturnsDeepStubs from mockito-scala. Here is a working example

import org.mockito.stubbing.ReturnsDeepStubs
import org.mockito.{ArgumentMatchersSugar, IdiomaticMockito}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

trait Bar {
  def zar: Int
}
trait Foo {
  def bar: Bar
}

class MockitoScalaDeepStubExampleSpec extends AnyFlatSpec with Matchers with IdiomaticMockito with ArgumentMatchersSugar {
  "mockito-scala" should "provide deep stubs" in {
    val foo = mock[Foo](ReturnsDeepStubs)
    foo.bar.zar returns 42
    foo.bar.zar should be (42)
  }
}
Mario Galic
  • 47,285
  • 6
  • 56
  • 98