In this Scala
class
class A{
def a() = {b();}
def b() = {...}
}
If I want to test a()
, is it possible to mock or stub b()
Mocks override all methods (usually with return null
unless explicitly stated to return something else) while here you want to override just one method. Since your class isn't final
it would be easier to do something like
val tested: A = new A {
override def b = ...
}
than mock it (actually mocking final
classes is also impossible without something like PowerMock)
val tested = mock[A]
(tested.b _) returning (...)
// tested.a returns null and ignores b
because mock
would also override a
making your tests useless. You could "fix" it by mocking a
to have the same implementation as the original... but this is absurd for many reasons.
So mocking method of a tested class with a mocking framework is possible but it's getting in the way rather than helping.