Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support member references in AssertJ assertThrows #330

Merged
merged 1 commit into from
Apr 15, 2023
Merged
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 @@ -67,23 +67,34 @@ private Supplier<JavaParser> assertionsParser(ExecutionContext ctx) {
}
private static final MethodMatcher ASSERT_THROWS_MATCHER = new MethodMatcher("org.junit.jupiter.api.Assertions assertThrows(..)");

private static final JavaType THROWING_CALLABLE_TYPE = JavaType.buildType("org.assertj.core.api.ThrowableAssert.ThrowingCallable");

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (ASSERT_THROWS_MATCHER.matches(mi) && mi.getArguments().size() == 2) {
J.Lambda lambdaArg = (J.Lambda) mi.getArguments().get(1);
lambdaArg = lambdaArg.withType(JavaType.buildType("org.assertj.core.api.ThrowableAssert.ThrowingCallable"));
mi = mi.withTemplate(
JavaTemplate
.builder(this::getCursor,
"assertThatExceptionOfType(#{any(java.lang.Class)}).isThrownBy(#{any(org.assertj.core.api.ThrowableAssert.ThrowingCallable)})")
.javaParser(assertionsParser(ctx))
.staticImports("org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType")
.build(),
mi.getCoordinates().replace(),
mi.getArguments().get(0), lambdaArg);
maybeAddImport("org.assertj.core.api.AssertionsForClassTypes", "assertThatExceptionOfType");
maybeRemoveImport("org.junit.jupiter.api.Assertions.assertThrows");
J executable = mi.getArguments().get(1);
if (executable instanceof J.Lambda) {
executable = ((J.Lambda) executable).withType(THROWING_CALLABLE_TYPE);
} else if (executable instanceof J.MemberReference) {
executable = ((J.MemberReference) executable).withType(THROWING_CALLABLE_TYPE);
} else {
executable = null;
}

if (executable != null) {
mi = mi.withTemplate(
JavaTemplate
.builder(this::getCursor,
"assertThatExceptionOfType(#{any(java.lang.Class)}).isThrownBy(#{any(org.assertj.core.api.ThrowableAssert.ThrowingCallable)})")
.javaParser(assertionsParser(ctx))
.staticImports("org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType")
.build(),
mi.getCoordinates().replace(),
mi.getArguments().get(0), executable);
maybeAddImport("org.assertj.core.api.AssertionsForClassTypes", "assertThatExceptionOfType");
maybeRemoveImport("org.junit.jupiter.api.Assertions.assertThrows");
}
}
return mi;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,39 @@ public void throwsExceptionWithSpecificType() {
)
);
}

@Test
void memberReference() {
//language=java
rewriteRun(
java(
"""
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class MemberReferenceTest {

public void throwsWithMemberReference() {
CompletableFuture<Boolean> future = new CompletableFuture<>();
assertThrows(ExecutionException.class, future::get);
}
}
""",
"""
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class MemberReferenceTest {

public void throwsWithMemberReference() {
CompletableFuture<Boolean> future = new CompletableFuture<>();
assertThatExceptionOfType(ExecutionException.class).isThrownBy(future::get);
}
}
"""
)
);
}
}