spock icon indicating copy to clipboard operation
spock copied to clipboard

Clarified strict mocks doc

Open mipo256 opened this issue 10 months ago • 6 comments

In reality, Mockito is really using stricts mocks by default since 4 major release. So I think it is worth updating this doc :)

mipo256 avatar Feb 14 '25 11:02 mipo256

Do you have evidence it actually is the default? You say it is the default since 4 major releases. The current major release is v5. But what you linked to says since v4 and also just that it is intended to What you linked to is just a "planned to be the default for v4 though.

And for example https://github.com/mockito/mockito/blob/main/mockito-core/src/main/java/org/mockito/MockSettings.java#L373 says the default is lenient. For https://github.com/mockito/mockito/blob/main/mockito-core/src/main/java/org/mockito/junit/MockitoJUnit.java#L34 it seems the default is warn, so behavioral still lenient. Didn't look further.

Vampire avatar Feb 14 '25 12:02 Vampire

It seems that you're right, I'm sorry. But at least, Mockito has planned to migrate to strict mocks for a very long time, initially it was planned to Mockito v3.

So I think it is at least it does not make much sense to say that Spock like Mockito relies on lenient mocks. Becuase Mockito does not recommend that.

mipo256 avatar Feb 17 '25 08:02 mipo256

Well, having a look at their docs I'd say even if it were enabled by default, it is not the strictness we talk about here.

As far as I understood those docs, even with using Mockito strict mode you still need to use verifyNoMoreInteractions to achieve the strictness we are talking about here.

It is "a bit stricter" by failing if you do stub a method and it gets called but with different arguments it fails and if you do stub a method and it is not called it fails. But that's imho merely a "mistyped" protection or "refactored" protection but not the strictness mentioned here where any call on the mock would fail. 🤷‍♂️

Vampire avatar Feb 17 '25 13:02 Vampire

@leonard84 what do you think?

Vampire avatar Feb 17 '25 13:02 Vampire

Unfortunately the term "strict" is used very differently. EasyMock for example also has a strict mode, that there means that also invocation order is checked.

Vampire avatar Feb 17 '25 13:02 Vampire

Calling Mockito's verifyNoMoreInteractions is basically the same as adding 0 * _ to a Spec.

I agree with @Vampire that "strictness" is not well-defined. We can remove the Like Mockito intro, but saying that Mockito is strict by default would be wrong, they just recommend it.

leonard84 avatar Feb 27 '25 15:02 leonard84