Skip to content

Commit 973d834

Browse files
committed
fix TestRuleToTestInfo scope check when inserting new before each method declaration.
1 parent 10e8635 commit 973d834

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,16 @@ public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext executionC
125125
return nc;
126126
}
127127

128-
@Override
129-
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
130-
J.MethodDeclaration md = super.visitMethodDeclaration(method, executionContext);
131-
if (md.getLeadingAnnotations().stream().anyMatch(this::isBeforeAnnotation)) {
132-
//noinspection ConstantConditions
133-
getCursor().putMessage("before-method", getCursor().firstEnclosing(J.ClassDeclaration.class));
134-
}
135-
return md;
136-
}
128+
//FIXME. add TestMethod statements.
129+
// @Override
130+
// public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
131+
// J.MethodDeclaration md = super.visitMethodDeclaration(method, executionContext);
132+
// return md;
133+
// }
137134

138135
private boolean isBeforeAnnotation(J.Annotation annotation) {
139136
return TypeUtils.isOfClassType(annotation.getType(), "org.junit.Before") || TypeUtils.isOfClassType(annotation.getType(), "org.junit.jupiter.api.BeforeEach");
140137
}
141-
142-
143138
};
144139
}
145140

@@ -155,7 +150,7 @@ public AddBeforeEachMethod(J.VariableDeclarations varDecls, J.ClassDeclaration e
155150
@Override
156151
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
157152
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext);
158-
if (getCursor().isScopeInPath(enclosingClass)) {
153+
if (enclosingClass.getId().equals(cd.getId())) {
159154
String t = "@BeforeEach\n" +
160155
"public void setup(TestInfo testInfo) {\n" +
161156
" Optional<Method> testMethod = testInfo.getTestMethod();\n" +

src/test/kotlin/org/openrewrite/java/testing/junit5/TestRuleToTestInfoTest.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class TestRuleToTestInfoTest : JavaRecipeTest {
3737
protected String randomName() {
3838
return name.getMethodName();
3939
}
40+
41+
private static class SomeInnerClass {
42+
}
4043
}
4144
""",
4245
after = """
@@ -48,6 +51,9 @@ class TestRuleToTestInfoTest : JavaRecipeTest {
4851
protected String randomName() {
4952
return name;
5053
}
54+
55+
private static class SomeInnerClass {
56+
}
5157
5258
@BeforeEach
5359
public void setup(TestInfo testInfo) {
@@ -59,4 +65,4 @@ class TestRuleToTestInfoTest : JavaRecipeTest {
5965
}
6066
"""
6167
)
62-
}
68+
}

0 commit comments

Comments
 (0)