Skip to content

Commit f7df211

Browse files
Add mockito-junit-jupiter when using runner in JUnit 4 (#670)
* [640] - check for junit4 mockito runners to add mockito-junit-jupiter * Update expectation after upstream v0.20.0 release of EPS * Use selective dependencies & run specific recipe * Drop the ineffective AddDependency from `Mockito1to3Migration` * Fix similar issue identified for Vertx --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 032b0eb commit f7df211

File tree

3 files changed

+100
-10
lines changed

3 files changed

+100
-10
lines changed

src/main/resources/META-INF/rewrite/junit5.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ tags:
196196
- junit
197197
- mockito
198198
recipeList:
199+
- org.openrewrite.java.dependencies.AddDependency:
200+
groupId: org.mockito
201+
artifactId: mockito-junit-jupiter
202+
version: 4.x
203+
onlyIfUsing: org.mockito..MockitoJUnit*Runner
204+
acceptTransitive: true
205+
scope: test
199206
- org.openrewrite.java.testing.mockito.Mockito1to4Migration
200207
- org.openrewrite.java.testing.mockito.MockitoJUnitRunnerSilentToExtension
201208
- org.openrewrite.java.testing.junit5.RunnerToExtension:
@@ -246,7 +253,7 @@ recipeList:
246253
groupId: io.vertx
247254
artifactId: vertx-junit5
248255
version: 4.x
249-
onlyIfUsing: io.vertx.junit5.VertxExtension
256+
onlyIfUsing: org.vertx.testtools.VertxUnitRunner
250257
acceptTransitive: true
251258
---
252259
type: specs.openrewrite.org/v1beta/recipe

src/main/resources/META-INF/rewrite/mockito.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,6 @@ recipeList:
168168
- org.openrewrite.java.testing.mockito.MockUtilsToStatic
169169
- org.openrewrite.java.testing.junit5.MockitoJUnitToMockitoExtension
170170
- org.openrewrite.java.testing.mockito.ReplacePowerMockito
171-
- org.openrewrite.java.dependencies.AddDependency:
172-
groupId: org.mockito
173-
artifactId: mockito-junit-jupiter
174-
version: 3.x
175-
onlyIfUsing: org.mockito.junit.jupiter.*
176-
acceptTransitive: true
177-
scope: test
178171
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
179172
groupId: org.mockito
180173
artifactId: "*"

src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import static org.assertj.core.api.Assertions.assertThat;
3232
import static org.openrewrite.gradle.Assertions.buildGradle;
3333
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
34-
import static org.openrewrite.java.Assertions.java;
34+
import static org.openrewrite.java.Assertions.*;
3535
import static org.openrewrite.maven.Assertions.pomXml;
3636

3737
@SuppressWarnings({"NewClassNamingConvention", "EqualsWithItself", "deprecation", "LanguageMismatch"})
@@ -40,7 +40,7 @@ class JUnit5MigrationTest implements RewriteTest {
4040
public void defaults(RecipeSpec spec) {
4141
spec
4242
.parser(JavaParser.fromJavaVersion()
43-
.classpathFromResources(new InMemoryExecutionContext(), "junit-4.13", "hamcrest-2.2"))
43+
.classpathFromResources(new InMemoryExecutionContext(), "junit-4.13"))
4444
.recipe(Environment.builder()
4545
.scanRuntimeClasspath("org.openrewrite.java.testing.junit5")
4646
.build()
@@ -81,6 +81,9 @@ void method() {
8181
void assertThatReceiver() {
8282
//language=java
8383
rewriteRun(
84+
spec -> spec
85+
.parser(JavaParser.fromJavaVersion()
86+
.classpathFromResources(new InMemoryExecutionContext(), "junit-4.13", "hamcrest-2.2")),
8487
java(
8588
"""
8689
import org.junit.Assert;
@@ -440,4 +443,91 @@ void bumpSurefireOnOlderMavenVersions() {
440443
)
441444
);
442445
}
446+
447+
@Test
448+
void addMockitoJupiterDependencyIfExtendWithPresent() {
449+
rewriteRun(
450+
spec -> spec
451+
.parser(JavaParser.fromJavaVersion()
452+
.classpathFromResources(new InMemoryExecutionContext(), "junit-4.13", "mockito-all-1.10"))
453+
.recipe(Environment.builder()
454+
.scanRuntimeClasspath("org.openrewrite.java.testing.junit5")
455+
.build()
456+
.activateRecipes("org.openrewrite.java.testing.junit5.UseMockitoExtension")),
457+
mavenProject("sample",
458+
//language=java
459+
srcMainJava(
460+
java(
461+
"""
462+
import org.junit.runner.RunWith;
463+
import org.mockito.runners.MockitoJUnitRunner;
464+
465+
@RunWith(MockitoJUnitRunner.class)
466+
public class MyClassTest {}
467+
""",
468+
"""
469+
import org.junit.jupiter.api.extension.ExtendWith;
470+
import org.mockito.junit.jupiter.MockitoExtension;
471+
472+
@ExtendWith(MockitoExtension.class)
473+
public class MyClassTest {}
474+
"""
475+
)
476+
),
477+
//language=xml
478+
pomXml(
479+
"""
480+
<project>
481+
<modelVersion>4.0.0</modelVersion>
482+
<groupId>org.example</groupId>
483+
<artifactId>project</artifactId>
484+
<version>0.0.1</version>
485+
<dependencies>
486+
<dependency>
487+
<groupId>junit</groupId>
488+
<artifactId>junit</artifactId>
489+
<version>4.12</version>
490+
<scope>test</scope>
491+
</dependency>
492+
<dependency>
493+
<groupId>org.mockito</groupId>
494+
<artifactId>mockito-core</artifactId>
495+
<version>2.23.4</version>
496+
<scope>test</scope>
497+
</dependency>
498+
</dependencies>
499+
</project>
500+
""",
501+
"""
502+
<project>
503+
<modelVersion>4.0.0</modelVersion>
504+
<groupId>org.example</groupId>
505+
<artifactId>project</artifactId>
506+
<version>0.0.1</version>
507+
<dependencies>
508+
<dependency>
509+
<groupId>junit</groupId>
510+
<artifactId>junit</artifactId>
511+
<version>4.12</version>
512+
<scope>test</scope>
513+
</dependency>
514+
<dependency>
515+
<groupId>org.mockito</groupId>
516+
<artifactId>mockito-core</artifactId>
517+
<version>4.11.0</version>
518+
<scope>test</scope>
519+
</dependency>
520+
<dependency>
521+
<groupId>org.mockito</groupId>
522+
<artifactId>mockito-junit-jupiter</artifactId>
523+
<version>4.11.0</version>
524+
<scope>test</scope>
525+
</dependency>
526+
</dependencies>
527+
</project>
528+
"""
529+
)
530+
)
531+
);
532+
}
443533
}

0 commit comments

Comments
 (0)