Skip to content

Conversation

@asolntsev
Copy link
Contributor

this commit adds method Mockito.mock() as a shorter alternative for Mockito.mock(Class). When result of this call is assigned to a variable/field, java will detect the needed class automatically.

P.S. I had to ignore errorprone checks "DoNotMock" and "DoNotMockAutoValue" because they fail with IndexOutOfBoundException trying to get the first argument of Mockito.mock(). :)

this commit adds method `Mockito.mock()` as a shorter alternative for `Mockito.mock(Class)`. When result of this call is assigned to a variable/field, java will detect the needed class automatically.

P.S. I had to ignore errorprone checks "DoNotMock" and "DoNotMockAutoValue" because they fail with IndexOutOfBoundException trying to get the first argument of `Mockito.mock()`. :)
@TimvdLippe
Copy link
Contributor

This is a super interesting implementation and I am not sure what to think of it. What I like about @Mock is that we don't have to deal with the unchecked warnings. This method would fix that exact problem, but now for mock(), which is awesome. At the same time, it also feels like a bit of a hack and I wonder what the readability of this will be. The expressiveness of the .class argument makes it at least clear what we are attempting to mock and what you will get.

That said, I am not sure if there is any harm in shipping this method, given that it does resolve the unchecked warning problem for Mockito.mock(), which is valuable on its own. For that, I think we do need to fix these 2 ErrorProne checks, as they will be crashing otherwise. However, I think that needs to be done after this PR is merged.

@mockito/developers WDYT?

@raphw
Copy link
Member

raphw commented Nov 11, 2022

I do like the concept! I am however not sure if overloading might create some issues here and there, but as our tests compile, it does not seem to be a problem. We are already doing a lot with the Java language that is not intuitive at first glance, so I think this would be a good API addition.

Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a new section to the JavaDoc (section 54) that showcases the new feature. Don't forget to also link to the new section in the Contents section at the top.

@szpak
Copy link
Member

szpak commented Nov 11, 2022

This API is similar to the Spock one where it has been in common use for years. If there are no warnings in IDE, why not to leverage it (assuming no technical glitches are found).

var mock = mock(MyClass.class)

is a (current) alternative, but the new one is even more robust (and shorter).

Btw, Groovy allows to do that kind of tricks, but I didn't know about that one in Java, good idea @asolntsev!

@codecov-commenter
Copy link

codecov-commenter commented Nov 13, 2022

Codecov Report

Base: 86.18% // Head: 74.95% // Decreases project coverage by -11.22% ⚠️

Coverage data is based on head (7232399) compared to base (eb85518).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@              Coverage Diff              @@
##               main    #2779       +/-   ##
=============================================
- Coverage     86.18%   74.95%   -11.23%     
+ Complexity     2828     2549      -279     
=============================================
  Files           320      320               
  Lines          8583     8556       -27     
  Branches       1060     1044       -16     
=============================================
- Hits           7397     6413      -984     
- Misses          905     1814      +909     
- Partials        281      329       +48     
Impacted Files Coverage Δ
src/main/java/org/mockito/Mockito.java 92.85% <100.00%> (-3.25%) ⬇️
...ckito/exceptions/stacktrace/StackTraceCleaner.java 0.00% <0.00%> (-100.00%) ⬇️
...ito/internal/stubbing/UnusedStubbingReporting.java 0.00% <0.00%> (-100.00%) ⬇️
.../exceptions/misusing/PotentialStubbingProblem.java 0.00% <0.00%> (-100.00%) ⬇️
...ernal/matchers/apachecommons/ReflectionEquals.java 0.00% <0.00%> (-100.00%) ⬇️
.../misusing/CannotStubVoidMethodWithReturnValue.java 0.00% <0.00%> (-100.00%) ⬇️
...mockito/internal/debugging/InvocationsPrinter.java 3.57% <0.00%> (-96.43%) ⬇️
.../org/mockito/internal/junit/ArgMismatchFinder.java 18.75% <0.00%> (-81.25%) ⬇️
.../mockito/internal/stubbing/StrictnessSelector.java 20.00% <0.00%> (-80.00%) ⬇️
...aultanswers/RetrieveGenericsForDefaultAnswers.java 18.86% <0.00%> (-79.25%) ⬇️
... and 103 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@asolntsev
Copy link
Contributor Author

Btw, Groovy allows to do that kind of tricks, but I didn't know about that one in Java, good idea @asolntsev!

I also didn't know that Java can detect the class name.
I found this trick in Tagir Valeev post

... similarly to `Mockito.mock()`
@asolntsev
Copy link
Contributor Author

@szpak @TimvdLippe Similarly to Mockito.mock(), I have also added method Mockito.spy().
Also implemented all suggestions from code reviews.

@asolntsev asolntsev requested a review from TimvdLippe November 13, 2022 10:52
Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 small nits. I wanted to fix them myself, but I didn't have permission to push to your fork. After that, this LGTM!

asolntsev and others added 2 commits November 14, 2022 22:08
Co-authored-by: Tim van der Lippe <