New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default mock of Optional
is not empty
when using RETURN_DEEP_STUBS
#2865
Comments
This should have been covered in mockito/src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsEmptyValues.java Line 132 in 0aba0d5
|
I'm seeing a similar test failures with deep stubs and methods returning
|
@schlosna, as far was I'm aware, this was not a regression, just an existing issue, effecting at least the latest 4.x and 5.x versions. What version were you updating from? |
I saw this in palantir/tritium#1635 when upgrading from Mockito 4.11.0 to 5.0.0 and running on JDK 17. I tried dropping the tests above into a mockito clone locally, but they did not reproduce. I can try a few more things to try to reproduce. |
@big-andy-coates I tried to fix this but eventually I came across an issue that if you have RETURNS_DEEP_STUBS, why would you expect an Optional to be not a mock? If it wouldn't be you would make some users quite surprised. Having List.isEmpty = false is a happy coincidence, because it is a mock and not a In case of Optional, you get one that is not |
Should we consider leaving it as a mock, but stubbing the |
…P_STUBS ReturnsDeepStubs now answers with true on Optional.isEmpty() when using RETURN_DEEP_STUBS. But the User can still mock the methods of Optional. In the normal mock case, for Optionals it returns as real Optional. Fixes mockito#2865
The PR #3097 would now return The behavior of |
…P_STUBS ReturnsDeepStubs now answers with true on Optional.isEmpty() when using RETURN_DEEP_STUBS. ReturnsDeepStubs now returns real Optionals instead of mocks. Fixes mockito#2865
…P_STUBS ReturnsDeepStubs now answers with true on Optional.isEmpty() when using RETURN_DEEP_STUBS. ReturnsDeepStubs now returns real Optionals instead of mocks. Fixes mockito#2865
…P_STUBS ReturnsDeepStubs now answers with true on Optional.isEmpty() when using RETURN_DEEP_STUBS. ReturnsDeepStubs now returns real Optionals instead of mocks. Fixes mockito#2865
ReturnsDeepStubs now answers with true on Optional.isEmpty() when using RETURN_DEEP_STUBS. ReturnsDeepStubs now returns real Optionals instead of mocks. Fixes #2865
Version affected: Mockito: 4.x & 5.x
If you create a mock with
RETURN_DEEP_STUBS
, then any method that returns a collection will, by default, return an empty collection, i.e.isEmpty
returnstrue
, etc. This is great, as it means no additional configuration of the mock is required before passing to logic that would attempt to access elements of the collection.However, the same is not true for
Optional
, (and I'm assumingOptionalLong
etc): ifRETURN_DEEP_STUBS
is used, methods that returnOptional
aren't treated in any special way and just return a default mock with not stubbing configured. This meansisEmpty
returnsfalse
, butget()
returnsnull
. This breaks the contract ofOptional
, causing test failures that aren't actually valid, or requiring manual configuration of theOptional
mock to do-the-right-thing, cluttering test code.Here's an example test that demonstrates this:
Note the second test, which doesn't use deep mocking, passes.
Note that some configuration are impossible to mock via Mockito
(same as any question on stackoverflow.com)
The text was updated successfully, but these errors were encountered: