Skip to content

Feat: add generic-inferred methods for constructing ArgumentCaptors#3144

Merged
TimvdLippe merged 3 commits intomockito:mainfrom
ascopes:feature/reified-argument-captor
Oct 12, 2023
Merged

Feat: add generic-inferred methods for constructing ArgumentCaptors#3144
TimvdLippe merged 3 commits intomockito:mainfrom
ascopes:feature/reified-argument-captor

Conversation

@ascopes
Copy link
Copy Markdown
Contributor

@ascopes ascopes commented Oct 12, 2023

Added a new method .captor(U...) on the ArgumentCaptor class that has the same interface as Mockito.mock(U...) with the goal of removing the need for unchecked-warning suppression when users make use of generic types in argument captors.

This allows the following usage without raising compiler warnings:

ArgumentCaptor<List<Thingy>> thingyCaptor = ArgumentCaptor.captor();

...rather than the current solution, which is less elegant...

@SuppressWarnings({"unchecked", "rawtypes"})
ArgumentCaptor<List<Thingy>> thingyCaptor = (ArgumentCaptor<List<Thingy>>) ArgumentCaptor.forClass(List.class);

A side effect of wanting to write tests for this meant I needed to expose the class held within a captor, so an additional method named .getCaptorType() has also been added. I have added this as public as it feels like a useful thing to expose anyway, given the user will know what is held internally. However, if this is unacceptable then I am happy to demote it to package-private or remove it and replace it with something like assertThat(captor).hasPropertyOrFieldWithValue("clazz", expectedClass);, or anything else that is suggested.

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/reified-argument-captor branch from 5e85696 to a09fa59 Compare October 12, 2023 08:37
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Oct 12, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (66acd09) 85.51% compared to head (4be5234) 85.52%.

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #3144   +/-   ##
=========================================
  Coverage     85.51%   85.52%           
- Complexity     2908     2912    +4     
=========================================
  Files           333      333           
  Lines          8852     8856    +4     
  Branches       1095     1095           
=========================================
+ Hits           7570     7574    +4     
  Misses          994      994           
  Partials        288      288           
Files Coverage Δ
src/main/java/org/mockito/ArgumentCaptor.java 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Added a new method .captor(U...) on the ArgumentCaptor class that
has the same interface as Mockito.mock(U...) with the goal of removing
the need for unchecked-warning suppression when users make use of
generic types in argument captors.
@ascopes ascopes force-pushed the feature/reified-argument-captor branch from a09fa59 to edc581d Compare October 12, 2023 08:42
@ascopes ascopes marked this pull request as ready for review October 12, 2023 08:54
Copy link
Copy Markdown
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.

Only 1 nit. The rest LGTM! Nice fix indeed.

Comment thread src/main/java/org/mockito/ArgumentCaptor.java Outdated
@ascopes ascopes force-pushed the feature/reified-argument-captor branch from c6511f7 to 4be5234 Compare October 12, 2023 17:34
@TimvdLippe TimvdLippe merged commit fd62884 into mockito:main Oct 12, 2023
@ascopes ascopes deleted the feature/reified-argument-captor branch October 13, 2023 09:52
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.

3 participants