Skip to content
Draft
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 @@ -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";
Expand All @@ -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 + " ";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 + " ";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* 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
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* 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();
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* 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
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* 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<Subject, Object> factory) {
assertAbout(factory).that(new Object());
}
}
""",
"""
import com.google.common.truth.Subject;

import static com.google.common.truth.Truth.assertAbout;

class Test {
void test(Subject.Factory<Subject, Object> factory) {
/* Truth's assertAbout() with custom subjects requires manual migration to AssertJ custom assertions */assertAbout(factory).that(new Object());
}
}
"""
)
);
}
}
Loading