Skip to content

Fixes #3622: MockitoExtension fails cleanup when aborted before setup #3623

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

Merged

Conversation

AndreKurait
Copy link
Contributor

Fixes #3622: MockitoExtension fails cleanup when aborted before setup

In Junit, extensions can throw org.opentest4j.TestAbortedException which will abort any subsequent setup steps while afterEach will still run.

When MockitoExtension is declared after a beforeEach extension that skips, mockito's beforeEach will not be executed but it's afterEach will causing an exception:

java.lang.NullPointerException: Cannot invoke "java.util.Set.forEach(java.util.function.Consumer)" because the return value of "org.junit.jupiter.api.extension.ExtensionContext$Store.remove(Object, java.lang.Class)" is null

	at org.mockito.junit.jupiter.MockitoExtension.afterEach(MockitoExtension.java:194)
...

e.g.

    @Test
    @ExtendWith({SkipTestBeforeEachExtension.class, MockitoExtension.class})
    void should_handle_skip_in_before_each() {
    }

    private static class SkipTestBeforeEachExtension implements BeforeEachCallback {
        @Override
        public void beforeEach(ExtensionContext context) {
            throw new TestAbortedException("Skipped test");
        }
    }

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
  • Mention Fixes #<issue number> in the description if relevant
  • At least one commit should mention Fixes #<issue number> if relevant

@codecov-commenter
Copy link

codecov-commenter commented Mar 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.46%. Comparing base (1764e62) to head (d2c88e9).

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #3623   +/-   ##
=========================================
  Coverage     86.46%   86.46%           
- Complexity     2952     2953    +1     
=========================================
  Files           340      340           
  Lines          8971     8972    +1     
  Branches       1103     1103           
=========================================
+ Hits           7757     7758    +1     
  Misses          934      934           
  Partials        280      280           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…e setup

Signed-off-by: Andre Kurait <akurait@amazon.com>
@AndreKurait AndreKurait force-pushed the FixExtensionBehaviorWithSkippedSetup branch from 2486db2 to d2c88e9 Compare March 26, 2025 00:32
@TimvdLippe TimvdLippe merged commit 4f469c8 into mockito:main Mar 26, 2025
18 checks passed
@TimvdLippe
Copy link
Contributor

Thank you!

@AndreKurait AndreKurait deleted the FixExtensionBehaviorWithSkippedSetup branch April 1, 2025 13:30
svc-squareup-copybara pushed a commit to cashapp/misk that referenced this pull request Apr 7, 2025
| Package | Type | Package file | Manager | Update | Change |
|---|---|---|---|---|---|
| [org.mockito:mockito-core](https://github.com/mockito/mockito) |
dependencies | misk/gradle/libs.versions.toml | gradle | minor |
`5.16.1` -> `5.17.0` |
| [software.amazon.awssdk:sdk-core](https://aws.amazon.com/sdkforjava) |
dependencies | misk/gradle/libs.versions.toml | gradle | patch |
`2.31.15` -> `2.31.16` |
| [software.amazon.awssdk:sqs](https://aws.amazon.com/sdkforjava) |
dependencies | misk/gradle/libs.versions.toml | gradle | patch |
`2.31.15` -> `2.31.16` |
|
[software.amazon.awssdk:dynamodb-enhanced](https://aws.amazon.com/sdkforjava)
| dependencies | misk/gradle/libs.versions.toml | gradle | patch |
`2.31.15` -> `2.31.16` |
| [software.amazon.awssdk:dynamodb](https://aws.amazon.com/sdkforjava) |
dependencies | misk/gradle/libs.versions.toml | gradle | patch |
`2.31.15` -> `2.31.16` |
| [software.amazon.awssdk:aws-core](https://aws.amazon.com/sdkforjava) |
dependencies | misk/gradle/libs.versions.toml | gradle | patch |
`2.31.15` -> `2.31.16` |
| [software.amazon.awssdk:bom](https://aws.amazon.com/sdkforjava) |
dependencies | misk/gradle/libs.versions.toml | gradle | patch |
`2.31.15` -> `2.31.16` |
| [software.amazon.awssdk:auth](https://aws.amazon.com/sdkforjava) |
dependencies | misk/gradle/libs.versions.toml | gradle | patch |
`2.31.15` -> `2.31.16` |

---

### Release Notes

<details>
<summary>mockito/mockito (org.mockito:mockito-core)</summary>

### [`v5.17.0`](https://github.com/mockito/mockito/releases/tag/v5.17.0)

<sup><sup>*Changelog generated by [Shipkit Changelog Gradle
Plugin](https://github.com/shipkit/shipkit-changelog)*</sup></sup>

##### 5.17.0

- 2025-04-04 - [7
commit(s)](mockito/mockito@v5.16.1...v5.17.0)
by Adrian Roos, Andre Kurait, Jan Ouwens, Rafael Winterhalter, Taeik
Lim, Thach Le, Tim van der Lippe
- Fixes [#&#8203;3631](mockito/mockito#3631):
Fix broken banner image link
[(#&#8203;3632)](mockito/mockito#3632)
- Banner image is broken
[(#&#8203;3631)](mockito/mockito#3631)
- Update exception message with mockito-inline
[(#&#8203;3628)](mockito/mockito#3628)
- Clarify structure of commit messages
[(#&#8203;3626)](mockito/mockito#3626)
- Fixes [#&#8203;3622](mockito/mockito#3622):
MockitoExtension fails cleanup when aborted before setup
[(#&#8203;3623)](mockito/mockito#3623)
- MockitoExtension fails cleanup when aborted before setup
[(#&#8203;3622)](mockito/mockito#3622)
- Since mockito-inline has been removed, the exception messages with
`mockito-inline` should be modified.
[(#&#8203;3621)](mockito/mockito#3621)
- Fixes [#&#8203;3171](mockito/mockito#3171):
Fall back to Throwable Location strategy on Android
[(#&#8203;3619)](mockito/mockito#3619)
- Fixes [#&#8203;3615](mockito/mockito#3615) :
broken links to javadoc.io
[(#&#8203;3616)](mockito/mockito#3616)
- Broken links to javadoc.io
[(#&#8203;3615)](mockito/mockito#3615)
- Mocks are not working on particular devices after update Android SDK
from 33 to 34
[(#&#8203;3171)](mockito/mockito#3171)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 6pm every weekday,before 2am
every weekday" in timezone Australia/Melbourne, Automerge - At any time
(no schedule defined).

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

♻ **Rebasing**: Never, or you tick the rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://github.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://github.com/renovatebot/renovate).

GitOrigin-RevId: 610e79d658b797b4e7b64d856b60fcbe8aa64650
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.

MockitoExtension fails cleanup when aborted before setup
3 participants