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

Feat: reified mock overloads #2882

Merged
merged 1 commit into from
Jan 21, 2023

Conversation

ascopes
Copy link
Contributor

@ascopes ascopes commented Jan 21, 2023

Added reified mock overload methods to match the methods that consume an explicit Class object.

This enables the reified overloads to provide the same functionalities as the older
explicit mock overloads.

A few examples of places where this would be overly useful:

interface AppendableBuilder<T extends Appendable> {
    AppendableBuilder append(Appendable other);
}

AppendableBuilder<StringBuilder> obj = mock(Answers.RETURNS_SELF);
List<String> inputArguments = mock("the input list");
List<String> outputArguments = mock("the output list");

...

assertThat(transformer.transform(inputArguments))
    .isSameAs(outputArguments);
    
// ERROR! Expected <the output list> but got <the input list>!

The following methods have been added:

  • Mockito.mock(String, T...) - Sets the mock name.
  • Mockito.mock(Answer, T...) - Sets the default answer.
  • Mockito.mock(WithSettings, T...) - Provides custom settings.

I have also added a case where passing null varargs in would result in a NullPointerException previously. Now, it will tell the user that they should not be providing these varargs at all.

These overloads should not conflict with any existing usages of this API in the intended way (that I can think of), as the assumption is that a user probably isn't going to want to be mocking Mockito internals, Strings, or WithSettings objects anyway. If they do need to achieve this, then this is still accessible via the existing Mockito.mock(Class) methods anyway.

Checklist

  • Read the contributing guide
  • PR should be motivated, i.e. what does it fix, why, and if relevant how
  • If possible / relevant include an example in the description, that could help all readers
    including project members to get a better picture of the change
  • Avoid other runtime dependencies
  • Meaningful commit history ; intention is important please rebase your commit history so that each
    commit is meaningful and help the people that will explore a change in 2 years
  • The pull request follows coding style

@ascopes ascopes force-pushed the feature/with-settings-reified branch from 0cec40b to 911dff3 Compare January 21, 2023 20:00
Added reified mock overload methods to match the methods
that consume an explicit Class<T> object.

The following methods have been added:

- Mockito.<T>mock(String, T...) - Sets the mock name.
- Mockito.<T>mock(Answer, T...) - Sets the default answer.
- Mockito.<T>mock(WithSettings, T...) - Provides custom settings.

I have also added a case where passing null varargs in would
result in a NullPointerException previously. Now, it will tell
the user that they should not be providing these varargs at all.

These overloads should not conflict with any existing usages of
this API in the intended way, as the assumption is that a user
probably isn't going to want to be mocking Mockito internals,
Strings, or WithSettings objects anyway. If they do need to
achieve this, then this is still accessible via the existing
Mockito.<T>mock(Class<T>) methods anyway.
@ascopes ascopes force-pushed the feature/with-settings-reified branch from 911dff3 to 7e25f17 Compare January 21, 2023 20:11
@codecov-commenter
Copy link

codecov-commenter commented Jan 21, 2023

Codecov Report

Base: 84.76% // Head: 84.76% // Increases project coverage by +0.00% 🎉

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

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #2882   +/-   ##
=========================================
  Coverage     84.76%   84.76%           
- Complexity     2868     2872    +4     
=========================================
  Files           327      327           
  Lines          8782     8785    +3     
  Branches       1068     1068           
=========================================
+ Hits           7444     7447    +3     
  Misses         1065     1065           
  Partials        273      273           
Impacted Files Coverage Δ
src/main/java/org/mockito/Mockito.java 96.55% <100.00%> (+0.12%) ⬆️

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.

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.

Makes sense, thank you!

@TimvdLippe TimvdLippe merged commit 91223f8 into mockito:main Jan 21, 2023
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

3 participants