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?
Asked
Active
Viewed 735 times
1 Answers
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
-
Having to mock the response of a mocked object shows why mocking isn't really a good idea never and that your design is more coupled that it should. – Luis Miguel Mejía Suárez Jun 01 '20 at 22:34