Skip to content

Commit

Permalink
Ignore disabled or empty tests in TestsShouldIncludeAssertions
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Sep 16, 2024
1 parent 13fcf55 commit 5be8a6f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,6 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {

private static class TestShouldIncludeAssertionsVisitor extends JavaIsoVisitor<ExecutionContext> {

JavaParser.Builder<?, ?> javaParser;

private JavaParser.Builder<?, ?> javaParser(ExecutionContext ctx) {
if (javaParser == null) {
javaParser = JavaParser.fromJavaVersion()
.classpathFromResources(ctx, "junit-jupiter-api-5.9");
}
return javaParser;
}

private final Map<String, Set<J.Block>> matcherPatternToClassInvocation = new HashMap<>();
private final List<String> additionalAsserts;

Expand All @@ -123,7 +113,8 @@ private static class TestShouldIncludeAssertionsVisitor extends JavaIsoVisitor<E
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext
ctx) {
if ((!methodIsTest(method) || method.getBody() == null) ||
if ((!methodIsTest(method) || method.getBody() == null || method.getBody().getStatements().isEmpty()) ||
methodIsDisabled(method) ||
methodHasAssertion(method.getBody()) ||
methodInvocationInBodyContainsAssertion()) {
return method;
Expand All @@ -135,7 +126,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
maybeAddImport("org.junit.jupiter.api.Assertions", "assertDoesNotThrow");
md = JavaTemplate.builder("assertDoesNotThrow(() -> #{any()});")
.staticImports("org.junit.jupiter.api.Assertions.assertDoesNotThrow")
.javaParser(javaParser(ctx))
.javaParser(JavaParser.fromJavaVersion()
.classpathFromResources(ctx, "junit-jupiter-api-5.9"))
.build()
.apply(updateCursor(md), md.getCoordinates().replaceBody(), body);
}
Expand All @@ -153,6 +145,15 @@ private boolean methodIsTest(J.MethodDeclaration methodDeclaration) {
return false;
}

private boolean methodIsDisabled(J.MethodDeclaration methodDeclaration) {
for (J.Annotation leadingAnnotation : methodDeclaration.getLeadingAnnotations()) {
if (TypeUtils.isOfClassType(leadingAnnotation.getType(), "org.junit.jupiter.api.Disabled")) {
return true;
}
}
return false;
}

private boolean methodHasAssertion(J.Block body) {
AtomicBoolean hasAssertion = new AtomicBoolean(Boolean.FALSE);
JavaIsoVisitor<AtomicBoolean> findAssertionVisitor = new JavaIsoVisitor<AtomicBoolean>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,42 @@ public void verifyTest() {
)
);
}

@Test
void ignoreDisabled() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
public class AaTest {
@Disabled
@Test
public void methodTest() {
Integer it = Integer.valueOf("2");
System.out.println(it);
}
}
"""
)
);
}

@Test
void ignoreEmpty() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Test;
public class AaTest {
@Test
public void methodTest() {
}
}
"""
)
);
}
}

0 comments on commit 5be8a6f

Please sign in to comment.