Skip to content

ReplaceInitMockToOpenMock: Ensure unique name for added tearDown method #725

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -107,7 +107,7 @@ private boolean isAnnotatedMethodPresent(J.ClassDeclaration cd, AnnotationMatche
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext ctx) {
if (!isAnnotatedMethodPresent(cd, AFTER_EACH_MATCHER) && isAnnotatedMethodPresent(cd, BEFORE_EACH_MATCHER)) {
maybeAddImport("org.junit.jupiter.api.AfterEach");
cd = JavaTemplate.builder("@AfterEach\nvoid tearDown() throws Exception {\n}")
cd = JavaTemplate.builder("@AfterEach\nvoid " + tearDownMethodName(cd) + "() throws Exception {\n}")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
.imports("org.junit.jupiter.api.AfterEach")
.build()
Expand Down Expand Up @@ -168,6 +168,18 @@ private J.MethodDeclaration addThrowsIfAbsent(J.MethodDeclaration md) {
JavaType.Class exceptionType = JavaType.ShallowClass.build(EXCEPTION_CLASS_NAME);
return md.withThrows(ListUtils.concat(md.getThrows(), new J.Identifier(randomId(), Space.SINGLE_SPACE, Markers.EMPTY, emptyList(), exceptionType.getClassName(), exceptionType, null)));
}

private String tearDownMethodName(J.ClassDeclaration cd) {
String methodName = "tearDown";
int suffix = 0;
String updatedMethodName = methodName;
for (Statement st : cd.getBody().getStatements()) {
if (st instanceof J.MethodDeclaration && ((J.MethodDeclaration) st).getSimpleName().equals(updatedMethodName)) {
updatedMethodName = methodName + suffix++;
}
}
return updatedMethodName;
}
};
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,75 @@ static class Helper {
);
}

@Test
void methodWithTearDownIsPresent() {
rewriteRun(
//language=java
java(
"""
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.MockitoAnnotations;

import static org.mockito.MockitoAnnotations.initMocks;

class A {

@BeforeEach
public void setUp() {
test1();
initMocks(this);
test2();
}

public void test1() {
}

public void test2() {
}

@AfterAll
public static void tearDown() {
}
}
""",
"""
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.MockitoAnnotations;

class A {

private AutoCloseable mocks;

@BeforeEach
public void setUp() {
test1();
mocks = MockitoAnnotations.openMocks(this);
test2();
}

public void test1() {
}

public void test2() {
}

@AfterAll
public static void tearDown() {
}

@AfterEach
void tearDown0() throws Exception {
mocks.close();
}
}
"""
)
);
}

@Test
void noChangesWithJunit4() {
rewriteRun(
Expand Down