Skip to content

Commit

Permalink
Keep String variables as argument in JUnitFailToAssertJFail too
Browse files Browse the repository at this point in the history
Fixes #504
  • Loading branch information
timtebeek committed Apr 15, 2024
1 parent 78e884a commit 90540b8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import java.util.List;

Expand Down Expand Up @@ -72,8 +73,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.javaParser(assertionsParser(ctx))
.build()
.apply(getCursor(), m.getCoordinates().replace());
} else if (args.get(0) instanceof J.Literal) {
m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(#{});")
} else if (args.get(0) instanceof J.Literal ||
TypeUtils.isAssignableTo("java.lang.String", args.get(0).getType())) {
m = JavaTemplate.builder("org.assertj.core.api.Assertions.fail(#{any()});")
.javaParser(assertionsParser(ctx))
.build()
.apply(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -249,4 +250,39 @@ public void test() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/504")
void stringVariableArgument() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class OpenrewriteTest {
@Test
public void smokeTest() {
String failMessage = "OpenrewriteTest.smokeTest() is not implemented yet";
Assertions.fail(failMessage);
}
}
""",
"""
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.fail;
public class OpenrewriteTest {
@Test
public void smokeTest() {
String failMessage = "OpenrewriteTest.smokeTest() is not implemented yet";
fail(failMessage);
}
}
"""
)
);
}
}

0 comments on commit 90540b8

Please sign in to comment.