Say I have trait:
trait A {
def a(): Boolean
}
And mock + testcase:
trait Fixture {
val mockA = mock[A]
(mockA _).returns(true)
}
"mock must return true" in new Fixture {
mockA() must equal(true)
}
Now, I want to re-mock the value to false for a single test in the same test suite
trait ChildFixture extends Fixture {
(mockA _).returns(false) // this obviously doesn't work
}
"mock must return false" in new ChildFixture {
mockA() must equal(false)
}
This is a very simplified example, I have a complex dependency chain of mocking fixtures. The obvious answer is to extract the mocking code for mockA
into another fixture and then mix into the ones that should be true, then make a new Fixture for the specific test that should be false. Only issue is this is an onerous process given my test code. It is also possible to manually mock the return with an anonymous class and a var, but this does not seem very elegant and loses the function of Scala mock call validation.
Is it possible to restub an already-mocked object for a single test?
override val
isn't an option? – Mateusz Kubuszokoverride val
but it unfortunately does not work. It causes a null pointer exception in the mock setup code in the parent fixture. – TTT