Skip to content

Commit

Permalink
Ensure MockitoRunnerToMockitoExtension leaves unrelated files untouch…
Browse files Browse the repository at this point in the history
…ed. Fixes #22
  • Loading branch information
sambsnyd committed Dec 16, 2020
1 parent 5ad7208 commit 8702fc6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,16 @@ public AnnotationUpdate() {

@Override
public J.ClassDecl visitClassDecl(J.ClassDecl cd) {
List<J.Annotation> annotations = cd.getAnnotations().stream()
.map(this::mockitoRunnerToMockitoExtension)
.collect(toList());
if(performedRefactor) {
boolean shouldReplaceAnnotation = cd.getAnnotations()
.stream()
.filter(this::shouldReplaceAnnotation)
.findAny()
.isPresent();
if(shouldReplaceAnnotation) {
performedRefactor = true;
List<J.Annotation> annotations = cd.getAnnotations().stream()
.map(this::mockitoRunnerToMockitoExtension)
.collect(toList());
return cd.withAnnotations(annotations);
}
return cd;
Expand All @@ -147,7 +153,7 @@ private J.Annotation mockitoRunnerToMockitoExtension(J.Annotation maybeMockitoAn
if(!shouldReplaceAnnotation(maybeMockitoAnnotation)) {
return maybeMockitoAnnotation;
}
performedRefactor = true;

Formatting originalFormatting = maybeMockitoAnnotation.getFormatting();

J.Annotation extendWithSpringExtension = extendWithMockitoExtensionAnnotation.withFormatting(originalFormatting);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@
*/
package org.openrewrite.java.testing.junit5

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test
import org.openrewrite.Refactor
import org.openrewrite.SourceFile
import org.openrewrite.*
import org.openrewrite.java.JavaParser
import org.openrewrite.java.tree.J
import org.openrewrite.maven.MavenParser
import org.openrewrite.maven.tree.Maven

class MockitoRunnerToMockitoExtensionTest {
class MockitoRunnerToMockitoExtensionTest: RefactorVisitorTestForParser<J.CompilationUnit> {

override val parser= JavaParser.fromJavaVersion()
.classpath("junit", "mockito")
.build()

override val visitors = listOf(MockitoRunnerToMockitoExtension())

@Test
fun replacesAnnotationAddsDependency() {
val jp = JavaParser.fromJavaVersion()
.classpath("junit", "mockito")
.build()
val mp = MavenParser.builder().build()

val sources: List<SourceFile> = jp.parse("""
val sources: List<SourceFile> = parser.parse("""
package org.openrewrite.java.testing.junit5;
import org.junit.Test;
Expand Down Expand Up @@ -65,7 +67,7 @@ class MockitoRunnerToMockitoExtensionTest {
""".trimIndent())

val changes = Refactor()
.visit(listOf(MockitoRunnerToMockitoExtension()))
.visit(visitors)
.fix(sources)


Expand Down Expand Up @@ -99,4 +101,80 @@ class MockitoRunnerToMockitoExtensionTest {
val mockitoJunitJupiterDep = actualPom.model.dependencies.find { it.artifactId == "mockito-junit-jupiter" }
assertNotNull(mockitoJunitJupiterDep)
}

@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/22")
@Test
fun leavesUnrelatedSourcesAlone() {
val shouldBeRefactored = """
package org.openrewrite.java.testing.junit5;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.List;
@RunWith(MockitoJUnitRunner.class)
public class ShouldBeRefactored {
@Mock
private List<Integer> list;
@Test
public void shouldDoSomething() {
list.add(100);
}
}
""".trimIndent()
val shouldBeLeftAlone = """
package org.openrewrite.java.testing.junit5;
import org.junit.Test;
import org.mockito.Mock;
import java.util.List;
public class ShouldBeLeftAlone {
@Mock
private List<Integer> list;
@Test
public void shouldDoSomething() {
list.add(100);
}
}
""".trimIndent()
val expectedShouldBeRefactored = """
package org.openrewrite.java.testing.junit5;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
@ExtendWith(MockitoExtension.class)
public class ShouldBeRefactored {
@Mock
private List<Integer> list;
@Test
public void shouldDoSomething() {
list.add(100);
}
}
""".trimIndent()

val changes = Refactor()
.visit(listOf(MockitoRunnerToMockitoExtension()))
.fix(parser.parse(shouldBeRefactored, shouldBeLeftAlone))

assertThat(changes.size).`as`("There should be exactly one change, made to class \"ShouldBeRefactored\"").isEqualTo(1)
val actualShouldBeRefactored = changes.first().fixed!! as J.CompilationUnit
assertThat(actualShouldBeRefactored.printTrimmed()).isEqualTo(expectedShouldBeRefactored)
}
}

0 comments on commit 8702fc6

Please sign in to comment.