Skip to content
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

Implements mocking for constructor invocations. #1975

Closed
wants to merge 1 commit into from

Conversation

raphw
Copy link
Member

@raphw raphw commented Jul 16, 2020

Allows to declare all constructor invocations as mocking attempts.

@codecov-commenter
Copy link

codecov-commenter commented Jul 16, 2020

Codecov Report

Merging #1975 into native-instantiation will decrease coverage by 0.18%.
The diff coverage is 78.82%.

Impacted file tree graph

@@                    Coverage Diff                     @@
##             native-instantiation    #1975      +/-   ##
==========================================================
- Coverage                   85.01%   84.82%   -0.19%     
- Complexity                   2617     2654      +37     
==========================================================
  Files                         323      324       +1     
  Lines                        7641     7877     +236     
  Branches                      928      947      +19     
==========================================================
+ Hits                         6496     6682     +186     
- Misses                        890      927      +37     
- Partials                      255      268      +13     
Impacted Files Coverage Δ Complexity Δ
src/main/java/org/mockito/MockedStatic.java 100.00% <ø> (ø) 1.00 <0.00> (ø)
...in/java/org/mockito/internal/MockedStaticImpl.java 78.66% <ø> (ø) 11.00 <0.00> (ø)
...nternal/creation/bytebuddy/ByteBuddyMockMaker.java 80.00% <0.00%> (-8.89%) 6.00 <0.00> (ø)
.../creation/bytebuddy/SubclassBytecodeGenerator.java 83.47% <0.00%> (-0.70%) 24.00 <0.00> (ø)
...reation/bytebuddy/inject/MockMethodDispatcher.java 0.00% <0.00%> (ø) 0.00 <0.00> (ø)
...nal/configuration/IndependentAnnotationEngine.java 85.00% <71.42%> (ø) 14.00 <7.00> (ø)
...a/org/mockito/internal/MockedConstructionImpl.java 72.22% <72.22%> (ø) 6.00 <6.00> (?)
...rc/main/java/org/mockito/internal/MockitoCore.java 93.18% <72.72%> (-1.86%) 43.00 <2.00> (+2.00) ⬇️
src/main/java/org/mockito/Mockito.java 91.02% <73.33%> (-4.22%) 55.00 <10.00> (+10.00) ⬇️
...l/creation/bytebuddy/InlineByteBuddyMockMaker.java 65.10% <74.33%> (+4.25%) 39.00 <11.00> (+10.00)
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 106ebcf...12ce7ea. Read the comment docs.

@raphw raphw force-pushed the constructor-mock branch 3 times, most recently from b19644e to b8b0468 Compare July 16, 2020 22:20
@raphw raphw force-pushed the constructor-mock branch 8 times, most recently from 5be3769 to 3f35be8 Compare July 17, 2020 16:15
@msteinbeck
Copy link

Are there any plans on when this will be released? I'm really looking forward for this feature.

@raphw
Copy link
Member Author

raphw commented Jul 22, 2020

Many of us are on vacation at the time but likely some time in August.

@raphw raphw force-pushed the native-instantiation branch 2 times, most recently from 3c95578 to 106ebcf Compare July 30, 2020 07:45
@JojOatXGME
Copy link
Contributor

JojOatXGME commented Aug 8, 2020

As far as I can see, there is still no support to get the arguments of the constructor. Is it planed to be added at some point? Here is a small motivation. I usually use copy constructors if I want to copy objects. (I think clone() is more or less broken.) If I want to assert that some mocked object is a copy of another object, I would need to look at the constructor arguments when the object is created.

Another suggestion: I think it would improve usability when MockedConstruction had a method to change the preparation callback. Otherwise, it is impossible to set a preparation callback in combination with using @Mock to create the instance.

@raphw raphw force-pushed the constructor-mock branch 2 times, most recently from 3dd8675 to 26699e7 Compare August 9, 2020 21:02
@raphw
Copy link
Member Author

raphw commented Aug 9, 2020

Good point, I refactored it to take a Context object in addition to the mock where one can get the invoked constructor including its arguments and an index for the count of the created mock for a given scope. This should offer any desired functionality.

@raphw
Copy link
Member Author

raphw commented Aug 15, 2020

Already on master.

@raphw raphw closed this Aug 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants