Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -133,6 +133,15 @@ private boolean isGroupableAssertion(J.MethodInvocation assertion) {
if (assertThatFq != null && assertionFq != null) {
return TypeUtils.isOfType(assertThatFq.getType(), assertionFq.getType());
}

// When assertion methods return SELF but the type parameter is not fully resolved,
// the return type may be a superclass (e.g. AbstractIterableAssert instead of ListAssert).
// Check if assertThatType is a subtype of assertionType to handle this case.
JavaType.FullyQualified assertThatFqType = TypeUtils.asFullyQualified(assertThatType);
JavaType.FullyQualified assertionFqType = TypeUtils.asFullyQualified(assertionType);
if (assertThatFqType != null && assertionFqType != null) {
return TypeUtils.isAssignableTo(assertionFqType.getFullyQualifiedName(), assertThatType);
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
new Object[]{actualArgument, expectedArgument};

return JavaTemplate.builder(template)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private J.MethodInvocation replace(J.MethodInvocation mi, ExecutionContext ctx)
(reasonArgument != null ? ".as(#{any(String)})" : "") +
".%s(%s)",
actual, assertion, argsTemplate))
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports(
"org.assertj.core.api.Assertions.assertThat",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private J.MethodInvocation handleTwoArgumentCase(J.MethodInvocation mi, Expressi
.collect(joining(", "));
JavaTemplate template = JavaTemplate.builder(String.format("assertThat(%s).%s(%s)",
actual, assertion, argumentsTemplate))
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build();
Expand Down Expand Up @@ -135,6 +136,7 @@ private J.MethodInvocation handleThreeArgumentCase(J.MethodInvocation mi, Expres
.collect(joining(", "));
JavaTemplate template = JavaTemplate.builder(String.format("assertThat(%s).as(#{any(String)}).%s(%s)",
actual, assertion, argumentsTemplate))
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.TypeValidation;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

Expand Down Expand Up @@ -132,7 +133,6 @@ void biscuits() {
class BiscuitTest {
void biscuits() {
List<String> biscuits = List.of("Ginger", "Chocolate", "Oatmeal");
assertThat(biscuits).isNotNull();
assertThat(biscuits)
.hasSize(3)
.contains("Chocolate");
Expand All @@ -143,6 +143,58 @@ void biscuits() {
);
}

@Test
void hamcrestToCollapsedAssertionsWithCustomType() {
rewriteRun(
spec -> spec
.typeValidationOptions(TypeValidation.none())
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "hamcrest-3")),
//language=java
java(
"""
class Biscuit {
String name;
Biscuit(String name) {
this.name = name;
}
}
"""
),
//language=java
java(
"""
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

class BiscuitTest {
void biscuits() {
List<Biscuit> biscuits = List.of(new Biscuit("Ginger"), new Biscuit("Chocolate"), new Biscuit("Oatmeal"));
assertThat(biscuits, hasSize(3));
assertThat(biscuits, hasItem(new Biscuit("Chocolate")));
assertThat(biscuits, not(hasItem(new Biscuit("Raisin"))));
}
}
""",
"""
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class BiscuitTest {
void biscuits() {
List<Biscuit> biscuits = List.of(new Biscuit("Ginger"), new Biscuit("Chocolate"), new Biscuit("Oatmeal"));
assertThat(biscuits).hasSize(3);
assertThat(biscuits).contains(new Biscuit("Chocolate"));
assertThat(biscuits).doesNotContain(new Biscuit("Raisin"));
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally these are collapsed as well.

}
}
"""
)
);
}

@Test
void junitCollapsedAssertions() {
rewriteRun(
Expand Down
Loading