Skip to content

Commit 0ce7cf5

Browse files
madlexaintellij-monorepo-bot
authored andcommitted
[junit5 inspection] support a default value for FieldSource and MethodSource IDEA-374913
GitOrigin-RevId: 3aee8076bc09ae22c39457d432403121591817df
1 parent a8245a1 commit 0ce7cf5

File tree

3 files changed

+96
-11
lines changed

3 files changed

+96
-11
lines changed

plugins/junit/java-tests/test/com/intellij/execution/junit/codeInspection/deadCode/JavaJunit5ImplicitUsageProviderTest.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,44 @@ class JavaJunit5ImplicitUsageProviderTest : JUnit5ImplicitUsageProviderTestBase(
161161
""".trimIndent())
162162
}
163163

164+
fun `test implicit usage of method in parameterized test`() {
165+
myFixture.testHighlighting(JvmLanguage.JAVA, """
166+
class MyTest {
167+
@org.junit.jupiter.api.Nested
168+
public class NewInnerTest extends InnerTest {
169+
public static String[] test() { return new String[]{"NewInner"}; }
170+
}
171+
172+
@org.junit.jupiter.api.Nested
173+
public class InnerTest {
174+
@org.junit.jupiter.params.ParameterizedTest
175+
@org.junit.jupiter.params.provider.MethodSource("test")
176+
void myTest(String param) { System.out.println(param); }
177+
public static String[] test() { return new String[]{"Inner"}; }
178+
}
179+
}
180+
""".trimIndent())
181+
}
182+
183+
fun `test implicit usage of field in parameterized test`() {
184+
myFixture.testHighlighting(JvmLanguage.JAVA, """
185+
class MyTest {
186+
@org.junit.jupiter.api.Nested
187+
public class NewInnerTest extends InnerTest {
188+
private static final String[] test = new String[]{"NewInner"};
189+
}
190+
191+
@org.junit.jupiter.api.Nested
192+
public class InnerTest {
193+
@org.junit.jupiter.params.ParameterizedTest
194+
@org.junit.jupiter.params.provider.FieldSource("test")
195+
void myTest(String param) { System.out.println(param); }
196+
private static final String[] test = new String[]{"Inner"};
197+
}
198+
}
199+
""".trimIndent())
200+
}
201+
164202
fun `test implicit usage of method source with implicit method name`() {
165203
myFixture.testHighlighting(JvmLanguage.JAVA, """
166204
import java.util.stream.*;

plugins/junit/kotlin-tests-shared/test/com/intellij/execution/junit/kotlin/codeInspection/deadCode/KotlinJUnit5ImplicitUsageProviderTest.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,50 @@ abstract class KotlinJUnit5ImplicitUsageProviderTest : JUnit5ImplicitUsageProvid
8383
""".trimIndent())
8484
}
8585

86+
fun `test implicit usage of method in parameterized test`() {
87+
myFixture.testHighlighting(JvmLanguage.KOTLIN, """
88+
class MyTest {
89+
class NewInnerTest: InnerTest() {
90+
companion object {
91+
@JvmStatic
92+
private fun test() = arrayOf("NewInner")
93+
}
94+
}
95+
96+
open class InnerTest {
97+
@org.junit.jupiter.params.ParameterizedTest
98+
@org.junit.jupiter.params.provider.MethodSource("test")
99+
fun myTest(param: String) { System.out.println(param) }
100+
companion object {
101+
@JvmStatic
102+
private fun test() = arrayOf("Inner")
103+
}
104+
}
105+
}
106+
""".trimIndent())
107+
}
108+
109+
fun `test implicit usage of field in parameterized test`() {
110+
myFixture.testHighlighting(JvmLanguage.KOTLIN, """
111+
class MyTest {
112+
class NewInnerTest: InnerTest() {
113+
companion object {
114+
private val test = arrayOf("InnerTest")
115+
}
116+
}
117+
118+
open class InnerTest {
119+
@org.junit.jupiter.params.ParameterizedTest
120+
@org.junit.jupiter.params.provider.FieldSource("test")
121+
fun myTest(param: String) { System.out.println(param) }
122+
companion object {
123+
private val test = arrayOf("Inner")
124+
}
125+
}
126+
}
127+
""".trimIndent())
128+
}
129+
86130
fun `test implicit usage of field source with implicit field name`() {
87131
myFixture.testHighlighting(JvmLanguage.KOTLIN, """
88132
import org.junit.jupiter.params.ParameterizedTest

plugins/junit/src/com/intellij/execution/junit/codeInspection/deadCode/JUnit5ImplicitUsageProvider.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private fun methodSourceIsImplicitlyUsed(element: PsiMethod): Boolean {
7474
if (parent is PsiClass) psiClass = parent
7575
}
7676

77-
return psiClass.methods.any { otherMethod ->
77+
return psiClass.allMethods.any { otherMethod ->
7878
MetaAnnotationUtil.isMetaAnnotated(otherMethod, setOf(ORG_JUNIT_JUPITER_PARAMS_PROVIDER_METHOD_SOURCE))
7979
&& MetaAnnotationUtil.isMetaAnnotated(otherMethod, setOf(ORG_JUNIT_JUPITER_PARAMS_PARAMETERIZED_TEST))
8080
&& isAnnotationMemberContainsName(methodName, otherMethod, ORG_JUNIT_JUPITER_PARAMS_PROVIDER_METHOD_SOURCE)
@@ -95,7 +95,7 @@ private fun fieldSourceIsImplicitlyUsed(element: PsiField): Boolean {
9595
if (parent is PsiClass) psiClass = parent
9696
}
9797

98-
return psiClass.methods.any { method ->
98+
return psiClass.allMethods.any { method ->
9999
MetaAnnotationUtil.isMetaAnnotated(method, setOf(ORG_JUNIT_JUPITER_PARAMS_PROVIDER_FIELD_SOURCE))
100100
&& MetaAnnotationUtil.isMetaAnnotated(method, setOf(ORG_JUNIT_JUPITER_PARAMS_PARAMETERIZED_TEST))
101101
&& isAnnotationMemberContainsName(fieldName, method, ORG_JUNIT_JUPITER_PARAMS_PROVIDER_FIELD_SOURCE)
@@ -111,14 +111,15 @@ private fun isAnnotationMemberContainsName(name: String, method: PsiMethod, anno
111111
val annotation = method.getAnnotation(annotationFqn) ?: return false
112112
val value = annotation.findAttributeValue(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME)
113113
if (value == null && method.name == name) return true
114-
if (value is PsiArrayInitializerMemberValue) {
115-
if (value.initializers.isEmpty() && method.name == name) return true
116-
for (memberValue in value.initializers) {
117-
if (memberValue is PsiLiteralExpression) {
118-
val data = JavaConstantExpressionEvaluator.computeConstantExpression(memberValue, null, false) as? String ?: continue
119-
if (data.isEmpty() && method.name == name) return true
120-
if (data == name) return true
121-
}
114+
115+
val initializers = if (value is PsiArrayInitializerMemberValue) value.initializers else arrayOf(value)
116+
// if an annotation value is empty, it's equivalent to the annotation value equal to the method name
117+
if (initializers.isEmpty() && method.name == name) return true
118+
for (memberValue in initializers) {
119+
if (memberValue is PsiLiteralExpression) {
120+
val data = JavaConstantExpressionEvaluator.computeConstantExpression(memberValue, null, false) as? String ?: continue
121+
if (data.isEmpty() && method.name == name) return true
122+
if (data == name) return true
122123
}
123124
}
124125
return false
@@ -132,7 +133,9 @@ class JUnit5ImplicitUsageProvider : ImplicitUsageProvider {
132133
|| (element is PsiField && fieldSourceIsImplicitlyUsed(element))
133134
}
134135

135-
override fun isImplicitRead(element: PsiElement): Boolean = false
136+
override fun isImplicitRead(element: PsiElement): Boolean {
137+
return element is PsiField && fieldSourceIsImplicitlyUsed(element)
138+
}
136139

137140
override fun isImplicitWrite(element: PsiElement): Boolean {
138141
return element is PsiField && MetaAnnotationUtil.isMetaAnnotated(element, setOf(ORG_JUNIT_JUPITER_API_IO_TEMPDIR))

0 commit comments

Comments
 (0)