Skip to content

Commit b224699

Browse files
author
Tim te Beek
authored
Migrate junit.framework.Assert (#256)
Fixes #190
1 parent 60b5240 commit b224699

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ public String getDescription() {
6161
return "Convert JUnit 4 `TestCase` to JUnit Jupiter.";
6262
}
6363

64-
@Override
65-
public Duration getEstimatedEffortPerOccurrence() {
66-
return Duration.ofMinutes(5);
67-
}
64+
@Override
65+
public Duration getEstimatedEffortPerOccurrence() {
66+
return Duration.ofMinutes(5);
67+
}
6868

6969
@Override
7070
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
@@ -78,6 +78,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon
7878
}
7979

8080
doAfterVisit(new UsesType<>("junit.framework.TestCase"));
81+
doAfterVisit(new UsesType<>("junit.framework.Assert"));
8182
return cu;
8283
}
8384
};
@@ -92,6 +93,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon
9293
doAfterVisit(new TestCaseVisitor());
9394
// ChangeType for org.junit.Assert method invocations because TestCase extends org.junit.Assert
9495
doAfterVisit(new ChangeType("junit.framework.TestCase", "org.junit.Assert", true));
96+
doAfterVisit(new ChangeType("junit.framework.Assert", "org.junit.Assert", true));
9597
doAfterVisit(new AssertToAssertions.AssertToAssertionsVisitor());
9698
doAfterVisit(new UseStaticImport("org.junit.jupiter.api.Assertions assert*(..)"));
9799
doAfterVisit(new UseStaticImport("org.junit.jupiter.api.Assertions fail*(..)"));

src/test/kotlin/org/openrewrite/java/testing/junit5/MigrateJunitTestCaseTest.kt renamed to src/test/kotlin/org/openrewrite/java/testing/junit5/MigrateJUnitTestCaseTest.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import org.openrewrite.java.JavaParser
2121
import org.openrewrite.java.JavaRecipeTest
2222

2323
@Suppress("JUnit3StyleTestMethodInJUnit4Class")
24-
class MigrateJunitTestCaseTest : JavaRecipeTest {
24+
class MigrateJUnitTestCaseTest : JavaRecipeTest {
2525
override val parser: JavaParser = JavaParser.fromJavaVersion()
2626
.classpath("junit", "hamcrest")
2727
.build()
@@ -203,6 +203,40 @@ class MigrateJunitTestCaseTest : JavaRecipeTest {
203203
"""
204204
)
205205

206+
@Test
207+
fun notTestCaseHasAssertAssertion() = assertChanged(
208+
before = """
209+
import org.junit.Test;
210+
211+
import static junit.framework.Assert.assertTrue;
212+
213+
class AaTest {
214+
@Test
215+
public void someTest() {
216+
assertTrue("assert message", isSameStuff("stuff"));
217+
}
218+
private boolean isSameStuff(String stuff) {
219+
return "stuff".equals(stuff);
220+
}
221+
}
222+
""",
223+
after = """
224+
import org.junit.Test;
225+
226+
import static org.junit.jupiter.api.Assertions.assertTrue;
227+
228+
class AaTest {
229+
@Test
230+
public void someTest() {
231+
assertTrue(isSameStuff("stuff"), "assert message");
232+
}
233+
private boolean isSameStuff(String stuff) {
234+
return "stuff".equals(stuff);
235+
}
236+
}
237+
"""
238+
)
239+
206240
@Test
207241
fun notTestCase() = assertUnchanged(
208242
before = """

0 commit comments

Comments
 (0)