Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,18 @@ public AddDependency.Accumulator getInitialValue(ExecutionContext ctx) {

@Override
public TreeVisitor<?, ExecutionContext> getScanner(AddDependency.Accumulator acc) {
return addJupiterDependency().getScanner(acc);
// The scanner sees the sources before `MigrateJUnitTestCase` rewrites them, so a
// codebase of `junit.framework.TestCase` subclasses uses no `org.junit` type yet.
TreeVisitor<?, ExecutionContext> usesJUnit4 = addJupiterDependency("org.junit..*").getScanner(acc);
TreeVisitor<?, ExecutionContext> usesJUnit3 = addJupiterDependency("junit.framework..*").getScanner(acc);
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
usesJUnit4.visit(tree, ctx);
usesJUnit3.visit(tree, ctx);
return tree;
}
};
}

@Override
Expand All @@ -73,8 +84,12 @@ public TreeVisitor<?, ExecutionContext> getVisitor(AddDependency.Accumulator acc
}

private static AddDependency addJupiterDependency() {
return addJupiterDependency("org.junit..*");
}

private static AddDependency addJupiterDependency(String onlyIfUsing) {
return new AddDependency("org.junit.jupiter", "junit-jupiter", "5.x", null,
"org.junit..*", null, null, null, null, null,
onlyIfUsing, null, null, null, null, null,
null, null, null, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,75 @@ void addToCompileScope() {
)
);
}

@Test
void doNotAddWithoutJUnit() {
rewriteRun(
mavenProject("project",
srcTestJava(
//language=java
java(
"""
public class NotATest {
public void addition() {
assert 4 == 2 + 2;
}
}
"""
)
),
pomXml(
//language=xml
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>project</artifactId>
<version>0.0.1</version>
</project>
"""
)
)
);
}

@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/1113")
@Test
void addWhenOnlyJUnit3TestCaseIsUsed() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-4")),
mavenProject("project",
srcTestJava(
//language=java
java(
"""
import junit.framework.TestCase;

public class LegacyTest extends TestCase {
public void testAddition() {
assertEquals(4, 2 + 2);
}
}
"""
)
),
pomXml(
//language=xml
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>project</artifactId>
<version>0.0.1</version>
</project>
""",
spec -> spec.after(pom -> {
return assertThat(pom)
.contains("junit-jupiter")
.contains("<scope>test</scope>").actual();
})
)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,58 @@ void excludeJunit4Dependency() {
);
}

@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/1113")
@Test
void addJupiterWhenOnlyTestCaseIsUsed() {
rewriteRun(
mavenProject("project",
srcTestJava(
//language=java
java(
"""
import junit.framework.TestCase;

public class LegacyTest extends TestCase {
public void testAddition() {
assertEquals(4, 2 + 2);
}
}
""",
spec -> spec.after(src -> {
return assertThat(src)
.contains("import org.junit.jupiter.api.Test;")
.doesNotContain("junit.framework").actual();
})
)
),
pomXml(
//language=xml
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>project</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
""",
spec -> spec.after(pom -> {
return assertThat(pom)
.contains("<artifactId>junit-jupiter</artifactId>")
.doesNotContain("<artifactId>junit</artifactId>").actual();
})
)
)
);
}

@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429")
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/850")
@Test
Expand Down