Skip to content

Commit 1a25e21

Browse files
amishra-utimtebeek
andauthored
ReplaceInitMockToOpenMock: Ensure unique name for added tearDown method (#725)
Co-authored-by: Tim te Beek <[email protected]>
1 parent c7f206a commit 1a25e21

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

src/main/java/org/openrewrite/java/testing/mockito/ReplaceInitMockToOpenMock.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private boolean isAnnotatedMethodPresent(J.ClassDeclaration cd, AnnotationMatche
107107
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext ctx) {
108108
if (!isAnnotatedMethodPresent(cd, AFTER_EACH_MATCHER) && isAnnotatedMethodPresent(cd, BEFORE_EACH_MATCHER)) {
109109
maybeAddImport("org.junit.jupiter.api.AfterEach");
110-
cd = JavaTemplate.builder("@AfterEach\nvoid tearDown() throws Exception {\n}")
110+
cd = JavaTemplate.builder("@AfterEach\nvoid " + tearDownMethodName(cd) + "() throws Exception {\n}")
111111
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
112112
.imports("org.junit.jupiter.api.AfterEach")
113113
.build()
@@ -168,6 +168,18 @@ private J.MethodDeclaration addThrowsIfAbsent(J.MethodDeclaration md) {
168168
JavaType.Class exceptionType = JavaType.ShallowClass.build(EXCEPTION_CLASS_NAME);
169169
return md.withThrows(ListUtils.concat(md.getThrows(), new J.Identifier(randomId(), Space.SINGLE_SPACE, Markers.EMPTY, emptyList(), exceptionType.getClassName(), exceptionType, null)));
170170
}
171+
172+
private String tearDownMethodName(J.ClassDeclaration cd) {
173+
String methodName = "tearDown";
174+
int suffix = 0;
175+
String updatedMethodName = methodName;
176+
for (Statement st : cd.getBody().getStatements()) {
177+
if (st instanceof J.MethodDeclaration && ((J.MethodDeclaration) st).getSimpleName().equals(updatedMethodName)) {
178+
updatedMethodName = methodName + suffix++;
179+
}
180+
}
181+
return updatedMethodName;
182+
}
171183
};
172184
}
173185
);

src/test/java/org/openrewrite/java/testing/mockito/ReplaceInitMockToOpenMockTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,75 @@ static class Helper {
470470
);
471471
}
472472

473+
@Test
474+
void methodWithTearDownIsPresent() {
475+
rewriteRun(
476+
//language=java
477+
java(
478+
"""
479+
import org.junit.jupiter.api.AfterAll;
480+
import org.junit.jupiter.api.BeforeEach;
481+
import org.mockito.MockitoAnnotations;
482+
483+
import static org.mockito.MockitoAnnotations.initMocks;
484+
485+
class A {
486+
487+
@BeforeEach
488+
public void setUp() {
489+
test1();
490+
initMocks(this);
491+
test2();
492+
}
493+
494+
public void test1() {
495+
}
496+
497+
public void test2() {
498+
}
499+
500+
@AfterAll
501+
public static void tearDown() {
502+
}
503+
}
504+
""",
505+
"""
506+
import org.junit.jupiter.api.AfterAll;
507+
import org.junit.jupiter.api.AfterEach;
508+
import org.junit.jupiter.api.BeforeEach;
509+
import org.mockito.MockitoAnnotations;
510+
511+
class A {
512+
513+
private AutoCloseable mocks;
514+
515+
@BeforeEach
516+
public void setUp() {
517+
test1();
518+
mocks = MockitoAnnotations.openMocks(this);
519+
test2();
520+
}
521+
522+
public void test1() {
523+
}
524+
525+
public void test2() {
526+
}
527+
528+
@AfterAll
529+
public static void tearDown() {
530+
}
531+
532+
@AfterEach
533+
void tearDown0() throws Exception {
534+
mocks.close();
535+
}
536+
}
537+
"""
538+
)
539+
);
540+
}
541+
473542
@Test
474543
void noChangesWithJunit4() {
475544
rewriteRun(

0 commit comments

Comments
 (0)