Skip to content

Commit 326c048

Browse files
magicwerkThomas Mauchtimtebeek
authored
improve recipe and add test (#449)
Co-authored-by: Thomas Mauch <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent dc61c73 commit 326c048

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

src/main/java/org/openrewrite/java/testing/junit5/AddMissingTestBeforeAfterAnnotations.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,14 @@ private static class AddMissingTestBeforeAfterAnnotationsVisitor extends JavaIso
6666
@Override
6767
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
6868
if (!method.hasModifier(J.Modifier.Type.Static) && !method.isConstructor()) {
69-
Optional<Method> superMethod = TypeUtils.findOverriddenMethod(method.getMethodType());
70-
if (superMethod.isPresent()) {
69+
Method currMethod = method.getMethodType();
70+
Optional<Method> superMethod = TypeUtils.findOverriddenMethod(currMethod);
71+
while (superMethod.isPresent()) {
7172
method = maybeAddMissingAnnotation(method, superMethod.get(), LifecyleAnnotation.BEFORE_EACH, ctx);
7273
method = maybeAddMissingAnnotation(method, superMethod.get(), LifecyleAnnotation.AFTER_EACH, ctx);
7374
method = maybeAddMissingAnnotation(method, superMethod.get(), LifecyleAnnotation.TEST, ctx);
75+
currMethod = superMethod.get();
76+
superMethod = TypeUtils.findOverriddenMethod(currMethod);
7477
}
7578
}
7679
return super.visitMethodDeclaration(method, ctx);

src/test/java/org/openrewrite/java/testing/junit5/AddMissingTestBeforeAfterAnnotationsTest.java

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void test() {
9292
)
9393
);
9494
}
95-
95+
9696
@Test
9797
void addMissingTestBeforeAfterAnnotationsIfNewFound() {
9898
//language=java
@@ -154,4 +154,98 @@ public void test() {
154154
);
155155
}
156156

157+
@Test
158+
void addMissingTestBeforeAfterAnnotationsIfExtended() {
159+
//language=java
160+
rewriteRun(
161+
java(
162+
"""
163+
import org.junit.jupiter.api.AfterEach;
164+
import org.junit.jupiter.api.BeforeEach;
165+
import org.junit.jupiter.api.Test;
166+
167+
public class AbstractTest {
168+
@BeforeEach
169+
public void before() {
170+
}
171+
172+
@AfterEach
173+
public void after() {
174+
}
175+
176+
@Test
177+
public void test() {
178+
}
179+
}
180+
"""
181+
),
182+
java(
183+
"""
184+
public class A extends AbstractTest {
185+
public void before() {
186+
}
187+
188+
public void after() {
189+
}
190+
191+
public void test() {
192+
}
193+
}
194+
""",
195+
"""
196+
import org.junit.jupiter.api.AfterEach;
197+
import org.junit.jupiter.api.BeforeEach;
198+
import org.junit.jupiter.api.Test;
199+
200+
public class A extends AbstractTest {
201+
@BeforeEach
202+
public void before() {
203+
}
204+
205+
@AfterEach
206+
public void after() {
207+
}
208+
209+
@Test
210+
public void test() {
211+
}
212+
}
213+
"""
214+
),
215+
java(
216+
"""
217+
public class B extends A {
218+
public void before() {
219+
}
220+
221+
public void after() {
222+
}
223+
224+
public void test() {
225+
}
226+
}
227+
""",
228+
"""
229+
import org.junit.jupiter.api.AfterEach;
230+
import org.junit.jupiter.api.BeforeEach;
231+
import org.junit.jupiter.api.Test;
232+
233+
public class B extends A {
234+
@BeforeEach
235+
public void before() {
236+
}
237+
238+
@AfterEach
239+
public void after() {
240+
}
241+
242+
@Test
243+
public void test() {
244+
}
245+
}
246+
"""
247+
)
248+
);
249+
}
250+
157251
}

0 commit comments

Comments
 (0)