|
15 | 15 | */ |
16 | 16 | package org.openrewrite.java.testing.junit5 |
17 | 17 |
|
| 18 | +import org.assertj.core.api.Assertions.assertThat |
18 | 19 | import org.junit.jupiter.api.Assertions.assertEquals |
19 | 20 | import org.junit.jupiter.api.Assertions.assertNotNull |
20 | 21 | import org.junit.jupiter.api.Test |
21 | | -import org.openrewrite.Refactor |
22 | | -import org.openrewrite.SourceFile |
| 22 | +import org.openrewrite.* |
23 | 23 | import org.openrewrite.java.JavaParser |
24 | 24 | import org.openrewrite.java.tree.J |
25 | 25 | import org.openrewrite.maven.MavenParser |
26 | 26 | import org.openrewrite.maven.tree.Maven |
27 | 27 |
|
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()) |
29 | 35 |
|
30 | 36 | @Test |
31 | 37 | fun replacesAnnotationAddsDependency() { |
32 | | - val jp = JavaParser.fromJavaVersion() |
33 | | - .classpath("junit", "mockito") |
34 | | - .build() |
35 | 38 | val mp = MavenParser.builder().build() |
36 | | - |
37 | | - val sources: List<SourceFile> = jp.parse(""" |
| 39 | + val sources: List<SourceFile> = parser.parse(""" |
38 | 40 | package org.openrewrite.java.testing.junit5; |
39 | 41 |
|
40 | 42 | import org.junit.Test; |
@@ -65,7 +67,7 @@ class MockitoRunnerToMockitoExtensionTest { |
65 | 67 | """.trimIndent()) |
66 | 68 |
|
67 | 69 | val changes = Refactor() |
68 | | - .visit(listOf(MockitoRunnerToMockitoExtension())) |
| 70 | + .visit(visitors) |
69 | 71 | .fix(sources) |
70 | 72 |
|
71 | 73 |
|
@@ -99,4 +101,80 @@ class MockitoRunnerToMockitoExtensionTest { |
99 | 101 | val mockitoJunitJupiterDep = actualPom.model.dependencies.find { it.artifactId == "mockito-junit-jupiter" } |
100 | 102 | assertNotNull(mockitoJunitJupiterDep) |
101 | 103 | } |
| 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 | + } |
102 | 180 | } |
0 commit comments