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

fix(deps): update dependency org.mockito:mockito-junit-jupiter to v5 #198

Closed
wants to merge 1 commit into from

Conversation

renovate[bot]
Copy link
Contributor

@renovate renovate bot commented Feb 25, 2023

Mend Renovate

This PR contains the following updates:

Package Change Age Adoption Passing Confidence
org.mockito:mockito-junit-jupiter 4.11.0 -> 5.1.1 age adoption passing confidence

⚠ Dependency Lookup Warnings ⚠

Warnings were logged while processing this repo. Please check the Dependency Dashboard for more information.


Release Notes

mockito/mockito

v5.1.1

Changelog generated by Shipkit Changelog Gradle Plugin

5.1.1
  • 2023-01-30 - 1 commit(s) by Andriy Redko
  • StackWalker.Option not found on Mockito 5.1.0 (#​2891)
  • StackWalker.Option not found on Mockito 5.1.0 (#​2890)

v5.1.0

Changelog generated by Shipkit Changelog Gradle Plugin

5.1.0
  • 2023-01-29 - 12 commit(s) by Andriy Redko, Ashley, Róbert Papp, Stephan Schroevers, Tim te Beek, dependabot[bot]
  • Fixes some mistakes and missing details in documentation (#​2889)
  • Bump com.diffplug.spotless from 6.13.0 to 6.14.0 (#​2888)
  • Clean up JDK-8 related code (#​2883)
  • Feat: reified mock overloads (#​2882)
  • Clean up JDK-8 related code (#​2879)
  • Bump assertj-core from 3.24.1 to 3.24.2 (#​2875)
  • Make sure the tests use mock maker with intended member accessor (#​2872)
  • Bump com.diffplug.spotless from 6.12.1 to 6.13.0 (#​2871)
  • Remove broken link from CONTRIBUTING.md (#​2870)
  • Update outdated badge 3.x to 5.x (#​2869)
  • Broken link in CONTRIBUTING.md (#​2868)
  • Set current version to 5.x in README and highlight changes (#​2867)
  • Annotate Mockito#{mock,spy}(T... reified) with @SafeVarargs (#​2866)
  • Make sure the tests use mock maker with intended member accessor (#​2855)
  • Improve examples for InOrder (#​2843)

v5.0.0

Mockito 5: prepare for future JDK versions

For a while now, we have seen an increase in problems/incompatibilities with recent versions of the JDK due to our usage of JVM-internal API.
Most notably, JDK 17 made some changes which are incompatible with the current subclass mockmaker.
Therefore, to prepare for the future of JDK, we are making some core changes to ensure Mockito keeps on working.

Switch the default mockmaker to mockito-inline

Back in Mockito 2.7.6, we published a new mockmaker based on the "inline bytecode" principle.
This mockmaker creates mocks manipulating bytecode equivalent within the original class such that its method implementations hook into the normal Mockito machinery.
As a comparison, the subclass mockmaker generates "real" subclasses for mocks, to mimic the same behavior.
While the approaches are similar, the inline mockmaker avoids certain restrictions that the JDK imposes.
For example, it does not violate module boundaries (introduced in JDK 9, but more heavily used in JDK 17) and avoids the leaking of the creation of the subclass.

Massive thanks to community member @​reta who implemented this change.

Note: this does not affect mockito-android nor testing on Android.

When should I still be using the subclass mockmaker?

There are legitimate remaining use cases for the subclass mockmaker.
For example, on the Graal VM's native image, the inline mockmaker will not work and the subclass mockmaker is the appropriate choice.
Additionally, if you would like to avoid mocking final classes, using the subclass mockmaker is a possibibility.
Note however that if you solely want to use the subclass mockmaker to avoid mocking final, you will run into the above mentioned issues on JDK 17+.
We want to leave this choice up to our users, which is why we will keep on supporting the subclass mockmaker.

If you want to use the subclass mockmaker instead, you can use the new mockito-subclass artifact (published on Maven Central along with all our other artifacts).

Update the minimum supported Java version to 11

Mockito 4 supports Java 8 and above.
Similar to other open source projects, we are moving away from JDK 8 and to newer versions.
The primary reason for moving away from JDK 8 is the increasing maintenance costs with keeping our own infrastructure working.
Lately we have been running into more and more JDK 8 breakages.
Additionally, while we want to support the newest JDK API's, our current solution to support both JDK 8 and newer versions causes issues with the SecurityManager.
Since we want Mockito to work on the newest version and more and more businesses adopting JDK 11, we have decided to make the switch as well.

Massive thanks to community member @​reta who implemented this change.

What should I do if I still run JDK 8?

For JDK 8 and below, you can keep on using Mockito 4.
This is similar to if you are using JDK 6, for which you can keep on using Mockito 2.
The changes in Mockito 5 (for now) are primarily focused on the latest JDK versions, which means the API differences between Mockito 4 and 5 are minimal.
However, over time this will most likely widen, so we do recommend adopting JDK 11 in the future.

New type() method on ArgumentMatcher

One of our most used public API's for customizing Mockito is the ArgumentMatcher interface.
The interface allows you to define a custom matcher, which you can pass into method arguments to provide more targeted matches.
One major shortcoming of the ArgumentMatcher was the lack of varargs support.
There were many, many issues filed related to varargs and Mockito unable to handle them.

Community member @​big-andy-coates put in a lot of effort to come up with an appropriate solution, including fully implementing and comparing 2 approaches.
Ultimately, we decided that introducing a new type() method on ArgumentMatcher is the best solution.
As a result, it is now possible to update your custom matchers to implement varargs support, if you so desire.
Note that ArgumentMatcher is still a @FunctionalInterface and can therefore still be written as a lambda.

Massive thanks to community member @​big-andy-coates who implemented this change.

What is the effect of this new method?

For varargs methods, there was previously a way to only match zero arguments, or two or more arguments, by using the exact number of matchers, i.e.

long call(String... args);

// Will match calls with exactly zero arguments:
when(mock.call()).thenReturn(0L);

// Will match calls with exactly two arguments:
when(mock.call(any(), any())).thenReturn(0L);

But following the pattern to match exactly one argument:

when(mock.call(any())).thenReturn(0L);

doesn't work, as any is "vararg aware", so Mockito matched the any against each element of the varargs parameter, meaning it will match any number of arguments, i.e. the above would of matched all of these:

mock.call();
mock.call("a");
mock.call("a", "b");

With the new type method, it's now possible to differentiate matching calls with any exact number of arguments, or to match any number of arguments.

// Match any number of arguments:
when(mock.call(any(String[].class))).thenReturn(1L);
// Match invocations with no arguments:
when(mock.call()).thenReturn(1L);
// Match invocations with exactly one argument:
when(mock.call(any())).thenReturn(1L);
// Alternative to match invocations with exactly one argument:
when(mock.call(any(String.class))).thenReturn(1L);
// Match invocations with exactly two arguments:
when(mock.call(any(), any())).thenReturn(1L);

Therefore, if you want to match 0 or more arguments, use any(String[].class).
If you want to match an exact number of arguments, use any(String.class) (and specify as many any matchers as arguments you want to match on).

In a similar fashion, the behavior of ArgumentCaptor.forClass has changed as well.
If you want to capture all arguments, use an ArgumentCaptor for String[], otherwise String:

// Will capture 1 string
@&#8203;Captor private ArgumentCaptor<String> captor;
// Will capture all strings
@&#8203;Captor private ArgumentCaptor<String[]> captor;

For more information, see the description and conversation in pull request 2835 and pull request 2807.

At the same time, ArgumentCaptor is now fully type-aware.
This allows for capturing specific subclasses on a generic method:

// Given:
int simpleMethod(Collection<?> arg);

// When:
mock.simpleMethod(Set.of());
mock.simpleMethod(List.of());

// Then:
ArgumentCaptor<Collection<?>> captor = ArgumentCaptor.forClass(List.class);
verify(mock).simpleMethod(captor.capture());
assertThat(captor.getAllValues()).containsExactly(List.of());
Do I need to implement this new method?

No, you don't need to.
Mockito 5 declares a default implementation, returning Void.type as the type of an ArgumentMatcher.
This essentially means that Mockito will not consider the type when handling varargs.
However, if you do return a specific type, Mockito will consider this when matching arguments.
As a result, this new method is not a source-breaking change, but is a bytecode-breaking change.
All code working on Mockito 4 should work as-is when recompiled with Mockito 5.


Configuration

📅 Schedule: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 Ignore: Close this PR and you won't be reminded about this update again.


  • If you want to rebase/retry this PR, check this box

This PR has been generated by Mend Renovate. View repository job log here.

@szpak
Copy link
Contributor

szpak commented Feb 25, 2023

Mockito 5 requires JDK 11+. To be revised once #171 is addressed.

@szpak szpak closed this Feb 25, 2023
@renovate
Copy link
Contributor Author

renovate bot commented Feb 25, 2023

Renovate Ignore Notification

Because you closed this PR without merging, Renovate will ignore this update. You will not get PRs for any future 5.x releases. But if you manually upgrade to 5.x then Renovate will re-enable minor and patch updates automatically.

If you accidentally closed this PR, or if you changed your mind: rename this PR to get a fresh replacement PR.

@renovate renovate bot deleted the renovate/major-mockito-monorepo branch February 25, 2023 21:49
@TWiStErRob TWiStErRob added this to the 3.0.0 milestone Jan 18, 2024
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.

2 participants