diff --git a/src/main/java/org/openrewrite/java/testing/truth/TruthAssertToAssertThat.java b/src/main/java/org/openrewrite/java/testing/truth/TruthAssertToAssertThat.java index 32466ec37..940bc2f3d 100644 --- a/src/main/java/org/openrewrite/java/testing/truth/TruthAssertToAssertThat.java +++ b/src/main/java/org/openrewrite/java/testing/truth/TruthAssertToAssertThat.java @@ -24,11 +24,12 @@ import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.J; -import org.openrewrite.marker.SearchResult; +import org.openrewrite.trait.Comments; public class TruthAssertToAssertThat extends Recipe { private static final MethodMatcher ASSERT_MATCHER = new MethodMatcher("com.google.common.truth.Truth assert_()"); + private static final String MANUAL_REVIEW = asCommentText("Truth's assert_() requires manual review for migration to AssertJ"); @Getter final String displayName = "Convert Truth `assert_()` to AssertJ"; @@ -44,13 +45,16 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); if (ASSERT_MATCHER.matches(mi)) { - // Truth's assert_() returns a StandardSubjectBuilder which is used differently - // For now, we'll mark this as needing manual review - return SearchResult.found(mi, "Truth's assert_() requires manual review for migration to AssertJ"); + return Comments.of(updateCursor(mi)).multilineComment(MANUAL_REVIEW); } return mi; } }); } + + // Pad the text so the comment renders as `/* text */` rather than `/*text*/` + private static String asCommentText(String message) { + return " " + message + " "; + } } diff --git a/src/main/java/org/openrewrite/java/testing/truth/TruthCustomSubjectsToAssertJ.java b/src/main/java/org/openrewrite/java/testing/truth/TruthCustomSubjectsToAssertJ.java index 45cd9bf97..32a3d8a77 100644 --- a/src/main/java/org/openrewrite/java/testing/truth/TruthCustomSubjectsToAssertJ.java +++ b/src/main/java/org/openrewrite/java/testing/truth/TruthCustomSubjectsToAssertJ.java @@ -24,11 +24,12 @@ import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.J; -import org.openrewrite.marker.SearchResult; +import org.openrewrite.trait.Comments; public class TruthCustomSubjectsToAssertJ extends Recipe { private static final MethodMatcher ASSERT_ABOUT = new MethodMatcher("com.google.common.truth.Truth assertAbout(..)"); + private static final String MANUAL_REVIEW = asCommentText("Truth's assertAbout() with custom subjects requires manual migration to AssertJ custom assertions"); @Getter final String displayName = "Migrate Truth custom subjects to AssertJ"; @@ -44,14 +45,16 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); if (ASSERT_ABOUT.matches(mi)) { - // Truth's assertAbout() is used for custom subjects - // AssertJ uses a different pattern with custom assertion classes - // This requires manual migration to create custom AssertJ assertion classes - return SearchResult.found(mi, "Truth's assertAbout() with custom subjects requires manual migration to AssertJ custom assertions"); + return Comments.of(updateCursor(mi)).multilineComment(MANUAL_REVIEW); } return mi; } }); } + + // Pad the text so the comment renders as `/* text */` rather than `/*text*/` + private static String asCommentText(String message) { + return " " + message + " "; + } } diff --git a/src/test/java/org/openrewrite/java/testing/truth/TruthAssertToAssertThatTest.java b/src/test/java/org/openrewrite/java/testing/truth/TruthAssertToAssertThatTest.java new file mode 100644 index 000000000..1d5a566ba --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/truth/TruthAssertToAssertThatTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2025 the original author or authors. + *
+ * Licensed under the Moderne Source Available License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://docs.moderne.io/licensing/moderne-source-available-license + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.testing.truth; + +import org.junit.jupiter.api.Test; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class TruthAssertToAssertThatTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new TruthAssertToAssertThat()) + .parser(JavaParser.fromJavaVersion() + .classpathFromResources(new InMemoryExecutionContext(), "guava", "truth")); + } + + @Test + void addsCommentOnce() { + rewriteRun( + spec -> spec.cycles(2).expectedCyclesThatMakeChanges(1), + //language=java + java( + """ + import static com.google.common.truth.Truth.assert_; + + class Test { + void test() { + assert_().fail(); + } + } + """, + """ + import static com.google.common.truth.Truth.assert_; + + class Test { + void test() { + /* Truth's assert_() requires manual review for migration to AssertJ */assert_().fail(); + } + } + """ + ) + ); + } +} diff --git a/src/test/java/org/openrewrite/java/testing/truth/TruthCustomSubjectsToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/truth/TruthCustomSubjectsToAssertJTest.java new file mode 100644 index 000000000..7bbcd7c9b --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/truth/TruthCustomSubjectsToAssertJTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2025 the original author or authors. + *
+ * Licensed under the Moderne Source Available License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://docs.moderne.io/licensing/moderne-source-available-license + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.testing.truth;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class TruthCustomSubjectsToAssertJTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new TruthCustomSubjectsToAssertJ())
+ .parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(), "guava", "truth"));
+ }
+
+ @Test
+ void addsCommentOnce() {
+ rewriteRun(
+ spec -> spec.cycles(2).expectedCyclesThatMakeChanges(1),
+ //language=java
+ java(
+ """
+ import com.google.common.truth.Subject;
+
+ import static com.google.common.truth.Truth.assertAbout;
+
+ class Test {
+ void test(Subject.Factory