Skip to content

Commit 8702fc6

Browse files
committed
Ensure MockitoRunnerToMockitoExtension leaves unrelated files untouched. Fixes #22
1 parent 5ad7208 commit 8702fc6

File tree

2 files changed

+98
-14
lines changed

2 files changed

+98
-14
lines changed

src/main/java/org/openrewrite/java/testing/junit5/MockitoRunnerToMockitoExtension.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,16 @@ public AnnotationUpdate() {
134134

135135
@Override
136136
public J.ClassDecl visitClassDecl(J.ClassDecl cd) {
137-
List<J.Annotation> annotations = cd.getAnnotations().stream()
138-
.map(this::mockitoRunnerToMockitoExtension)
139-
.collect(toList());
140-
if(performedRefactor) {
137+
boolean shouldReplaceAnnotation = cd.getAnnotations()
138+
.stream()
139+
.filter(this::shouldReplaceAnnotation)
140+
.findAny()
141+
.isPresent();
142+
if(shouldReplaceAnnotation) {
143+
performedRefactor = true;
144+
List<J.Annotation> annotations = cd.getAnnotations().stream()
145+
.map(this::mockitoRunnerToMockitoExtension)
146+
.collect(toList());
141147
return cd.withAnnotations(annotations);
142148
}
143149
return cd;
@@ -147,7 +153,7 @@ private J.Annotation mockitoRunnerToMockitoExtension(J.Annotation maybeMockitoAn
147153
if(!shouldReplaceAnnotation(maybeMockitoAnnotation)) {
148154
return maybeMockitoAnnotation;
149155
}
150-
performedRefactor = true;
156+
151157
Formatting originalFormatting = maybeMockitoAnnotation.getFormatting();
152158

153159
J.Annotation extendWithSpringExtension = extendWithMockitoExtensionAnnotation.withFormatting(originalFormatting);

src/test/kotlin/org/openrewrite/java/testing/junit5/MockitoRunnerToMockitoExtensionTest.kt

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,28 @@
1515
*/
1616
package org.openrewrite.java.testing.junit5
1717

18+
import org.assertj.core.api.Assertions.assertThat
1819
import org.junit.jupiter.api.Assertions.assertEquals
1920
import org.junit.jupiter.api.Assertions.assertNotNull
2021
import org.junit.jupiter.api.Test
21-
import org.openrewrite.Refactor
22-
import org.openrewrite.SourceFile
22+
import org.openrewrite.*
2323
import org.openrewrite.java.JavaParser
2424
import org.openrewrite.java.tree.J
2525
import org.openrewrite.maven.MavenParser
2626
import org.openrewrite.maven.tree.Maven
2727

28-
class MockitoRunnerToMockitoExtensionTest {
28+
class MockitoRunnerToMockitoExtensionTest: RefactorVisitorTestForParser<J.CompilationUnit> {
29+
30+
override val parser= JavaParser.fromJavaVersion()
31+
.classpath("junit", "mockito")
32+
.build()
33+
34+
override val visitors = listOf(MockitoRunnerToMockitoExtension())
2935

3036
@Test
3137
fun replacesAnnotationAddsDependency() {
32-
val jp = JavaParser.fromJavaVersion()
33-
.classpath("junit", "mockito")
34-
.build()
3538
val mp = MavenParser.builder().build()
36-
37-
val sources: List<SourceFile> = jp.parse("""
39+
val sources: List<SourceFile> = parser.parse("""
3840
package org.openrewrite.java.testing.junit5;
3941
4042
import org.junit.Test;
@@ -65,7 +67,7 @@ class MockitoRunnerToMockitoExtensionTest {
6567
""".trimIndent())
6668

6769
val changes = Refactor()
68-
.visit(listOf(MockitoRunnerToMockitoExtension()))
70+
.visit(visitors)
6971
.fix(sources)
7072

7173

@@ -99,4 +101,80 @@ class MockitoRunnerToMockitoExtensionTest {
99101
val mockitoJunitJupiterDep = actualPom.model.dependencies.find { it.artifactId == "mockito-junit-jupiter" }
100102
assertNotNull(mockitoJunitJupiterDep)
101103
}
104+
105+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/22")
106+
@Test
107+
fun leavesUnrelatedSourcesAlone() {
108+
val shouldBeRefactored = """
109+
package org.openrewrite.java.testing.junit5;
110+
111+
import org.junit.Test;
112+
import org.junit.runner.RunWith;
113+
import org.mockito.Mock;
114+
import org.mockito.runners.MockitoJUnitRunner;
115+
116+
import java.util.List;
117+
118+
@RunWith(MockitoJUnitRunner.class)
119+
public class ShouldBeRefactored {
120+
121+
@Mock
122+
private List<Integer> list;
123+
124+
@Test
125+
public void shouldDoSomething() {
126+
list.add(100);
127+
}
128+
}
129+
""".trimIndent()
130+
val shouldBeLeftAlone = """
131+
package org.openrewrite.java.testing.junit5;
132+
133+
import org.junit.Test;
134+
import org.mockito.Mock;
135+
136+
import java.util.List;
137+
138+
public class ShouldBeLeftAlone {
139+
140+
@Mock
141+
private List<Integer> list;
142+
143+
@Test
144+
public void shouldDoSomething() {
145+
list.add(100);
146+
}
147+
}
148+
""".trimIndent()
149+
val expectedShouldBeRefactored = """
150+
package org.openrewrite.java.testing.junit5;
151+
152+
import org.junit.Test;
153+
import org.junit.jupiter.api.extension.ExtendWith;
154+
import org.mockito.Mock;
155+
import org.mockito.junit.jupiter.MockitoExtension;
156+
157+
import java.util.List;
158+
159+
@ExtendWith(MockitoExtension.class)
160+
public class ShouldBeRefactored {
161+
162+
@Mock
163+
private List<Integer> list;
164+
165+
@Test
166+
public void shouldDoSomething() {
167+
list.add(100);
168+
}
169+
}
170+
""".trimIndent()
171+
172+
val changes = Refactor()
173+
.visit(listOf(MockitoRunnerToMockitoExtension()))
174+
.fix(parser.parse(shouldBeRefactored, shouldBeLeftAlone))
175+
176+
assertThat(changes.size).`as`("There should be exactly one change, made to class \"ShouldBeRefactored\"").isEqualTo(1)
177+
val actualShouldBeRefactored = changes.first().fixed!! as J.CompilationUnit
178+
assertThat(actualShouldBeRefactored.printTrimmed()).isEqualTo(expectedShouldBeRefactored)
179+
}
102180
}

0 commit comments

Comments
 (0)