Skip to content

Commit

Permalink
improve recipe and add test (#449)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Mauch <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored Jan 4, 2024
1 parent dc61c73 commit 326c048
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ private static class AddMissingTestBeforeAfterAnnotationsVisitor extends JavaIso
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
if (!method.hasModifier(J.Modifier.Type.Static) && !method.isConstructor()) {
Optional<Method> superMethod = TypeUtils.findOverriddenMethod(method.getMethodType());
if (superMethod.isPresent()) {
Method currMethod = method.getMethodType();
Optional<Method> superMethod = TypeUtils.findOverriddenMethod(currMethod);
while (superMethod.isPresent()) {
method = maybeAddMissingAnnotation(method, superMethod.get(), LifecyleAnnotation.BEFORE_EACH, ctx);
method = maybeAddMissingAnnotation(method, superMethod.get(), LifecyleAnnotation.AFTER_EACH, ctx);
method = maybeAddMissingAnnotation(method, superMethod.get(), LifecyleAnnotation.TEST, ctx);
currMethod = superMethod.get();
superMethod = TypeUtils.findOverriddenMethod(currMethod);
}
}
return super.visitMethodDeclaration(method, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void test() {
)
);
}

@Test
void addMissingTestBeforeAfterAnnotationsIfNewFound() {
//language=java
Expand Down Expand Up @@ -154,4 +154,98 @@ public void test() {
);
}

@Test
void addMissingTestBeforeAfterAnnotationsIfExtended() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class AbstractTest {
@BeforeEach
public void before() {
}
@AfterEach
public void after() {
}
@Test
public void test() {
}
}
"""
),
java(
"""
public class A extends AbstractTest {
public void before() {
}
public void after() {
}
public void test() {
}
}
""",
"""
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class A extends AbstractTest {
@BeforeEach
public void before() {
}
@AfterEach
public void after() {
}
@Test
public void test() {
}
}
"""
),
java(
"""
public class B extends A {
public void before() {
}
public void after() {
}
public void test() {
}
}
""",
"""
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class B extends A {
@BeforeEach
public void before() {
}
@AfterEach
public void after() {
}
@Test
public void test() {
}
}
"""
)
);
}

}

0 comments on commit 326c048

Please sign in to comment.