Closed MyDogTom closed 5 years ago
First of all, thanks for your detailed proposal!
The STRICT_STUBS
feature definitely seems like the way to go.
I'm not really sure however about the lazyMock
solution.
It would be just as easy to write val mock by lazy { mock<Foo>() }
, right?
I feel like adding lazyMock
introduces another 'layer of complexity' to the library possibly leading to confusion, whereas using by lazy { mock() }
seems like a explicit decision of the test author.
Do you know if the folks over at Mockito have run into this problem? In plain Java you should be able to write private Foo foo = Mockito.mock(Foo.class);
as well, leading to the same problem?
Yes, you are right val mock by lazy { mock<Foo>() }
is enough. It's a bit more verbose, but I agree it's more explicit.
I asked also Mockito people, but no answer so far. My question on StackOverflow: https://stackoverflow.com/q/52345881/2711056 My question in mailing list (asked today): https://groups.google.com/forum/#!topic/mockito/X7jkA4BdzSk
Strictness.STRICT_STUBS
from JavaDoc
Example without
Strictness.STRICT_STUBS
I have the following interface and class.
Test, might look like that. But it has several problems (see comments):
Another problem. When I change
booUnderTest.doBoo(5)
tobooUnderTest.doBoo(6)
, I receive an errorWhich doesn't point me directly to the problem.
Example with
Strictness.STRICT_STUBS
Mockito tells me, that I have unused configuration :
This allows me to keep my tests clean. And If I change
booUnderTest.doBoo(5)
tobooUnderTest.doBoo(6)
, Mockito tells me:This points me to exact problem.
Another reason to use
Strictness.STRICT_STUBS
Source. I'd like to already write tests, that are ready bot Mockit 3.0.
What's missing?
In second example, I had to remove
val mockedFoo: Foo = mock()
and callmock
insidesetUp
method. This is because mock should be created afterRule
is executed.Proposal
Create a
lazy
variant ofMocking.mock
. Something likelazyMock
:This allows test like that:
Questions
STRICT_STUBS
and create mock when field is defined)