Add mockito-junit-jupiter dependency when MockitoExtension is introduced#941
Merged
Add mockito-junit-jupiter dependency when MockitoExtension is introduced#941
Conversation
…n.class) is introduced The Mockito1to3Migration recipe adds @ExtendWith(MockitoExtension.class) to JUnit 5 tests using Mockito annotations, but does not add the org.mockito:mockito-junit-jupiter dependency that provides MockitoExtension. This causes compilation failures when the dependency is not already on the classpath. Add a new ScanningRecipe (AddMockitoJupiterDependency) that detects the same preconditions and adds the dependency as a test-scoped artifact. A declarative YAML recipe using AddDependency with onlyIfUsing was not viable because within a recipe list cycle all scan phases run before all edit phases. The AddDependency scan would check for MockitoExtension usage before AddMockitoExtensionIfAnnotationsUsed has added it, so the type is never found.
5df6b31 to
8e681af
Compare
timtebeek
reviewed
Mar 24, 2026
|
|
||
| @Override | ||
| public AtomicBoolean getInitialValue(ExecutionContext ctx) { | ||
| return new AtomicBoolean(false); |
Member
There was a problem hiding this comment.
An AtomicBoolean Accumulator always makes me doubt we correctly handle multi module projects. Can we make this a mapping from JavaProject marker to a Boolean instead?
Replace AtomicBoolean with a Map<JavaProject, Boolean> accumulator so the dependency is only added to modules that actually need it in multi-module projects.
timtebeek
approved these changes
Mar 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Mockito1to3Migrationadds@ExtendWith(MockitoExtension.class)to JUnit 5 tests using Mockito annotations (@Mock,@Spy,@Captor,@InjectMocks), but does not add theorg.mockito:mockito-junit-jupiterdependency that providesMockitoExtension. This causes compilation failures post-migration when the dependency isn't already on the classpath.AddMockitoJupiterDependencyScanningRecipethat detects the same preconditions (Mockito annotations + JUnit 5 types + no existing@ExtendWith(MockitoExtension.class)) and addsmockito-junit-jupiteras a test-scoped dependency.Mockito1to3Migrationimmediately afterAddMockitoExtensionIfAnnotationsUsed. TheMockito1to4MigrationandMockito1to5Migrationchains inherit this and theirUpgradeDependencyVersionsteps upgrade the version accordingly.Why not a declarative YAML recipe?
A declarative
AddDependencywithonlyIfUsing: org.mockito.junit.jupiter.MockitoExtensionwas not viable. Within a YAML recipe list cycle, allScanningRecipescan phases run before all edit phases. TheAddDependencyscan would check forMockitoExtensionusage beforeAddMockitoExtensionIfAnnotationsUsedhas added@ExtendWith(MockitoExtension.class), so the type is never found and the dependency is never added. An imperativeScanningRecipewith a custom scanner that checks the original source preconditions solves this.