junit 5: don't warn about implicit injected parameters or methods with explicit ExtendWith annotation (IDEA-172259)

This commit is contained in:
Anna Kozlova
2017-05-02 22:29:34 +03:00
parent 1c941a1150
commit 8e42e396da
4 changed files with 31 additions and 3 deletions
@@ -38,4 +38,7 @@ public class JUnitCommonClassNames {
public static final String ORG_JUNIT_JUPITER_API_TEST = "org.junit.jupiter.api.Test";
public static final String ORG_JUNIT_JUPITER_API_REPEATED_TEST = "org.junit.jupiter.api.RepeatedTest";
public static final String ORG_JUNIT_JUPITER_API_REPETITION_INFO = "org.junit.jupiter.api.RepetitionInfo";
public static final String ORG_JUNIT_JUPITER_API_TEST_INFO = "org.junit.jupiter.api.TestInfo";
public static final String ORG_JUNIT_JUPITER_API_TEST_REPORTER = "org.junit.jupiter.api.TestReporter";
public static final String ORG_JUNIT_JUPITER_API_EXTENSION_EXTEND_WITH = "org.junit.jupiter.api.extension.ExtendWith";
}
@@ -16,6 +16,7 @@
package com.intellij.execution.junit.codeInsight
import com.intellij.codeInsight.AnnotationUtil
import com.intellij.codeInsight.MetaAnnotationUtil
import com.intellij.codeInsight.daemon.impl.analysis.JavaGenericsUtil
import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix
import com.intellij.codeInsight.intention.QuickFixFactory
@@ -33,6 +34,7 @@ import com.intellij.util.containers.ContainerUtil
import com.siyeh.InspectionGadgetsBundle
import com.siyeh.ig.junit.JUnitCommonClassNames
import org.jetbrains.annotations.Nls
import java.util.*
class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool() {
@@ -84,8 +86,8 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool(
if (valuesSource == null && enumSource == null && noMultiArgsProvider) {
holder.registerProblem(parameterizedAnnotation, "No sources are provided, the suite would be empty")
}
else if (method.parameterList.parametersCount > 1 && noMultiArgsProvider) {
holder.registerProblem(valuesSource ?: enumSource!!, "Multiple parameters are not supported by this source")
else if (noMultiArgsProvider && hasMultipleParameters(method)) {
holder.registerProblem(valuesSource ?: enumSource!!, "Multiple parameters are not supported by this source")
}
}
}
@@ -147,7 +149,7 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool(
holder.registerProblem(attributeValue,
"Method source \'$providerName\' must have one of the following return type: Stream<?>, Iterator<?>, Iterable<?> or Object[]")
}
else if (method.parameterList.parametersCount > 1 && !isArgumentsInheritor(componentType)) {
else if (hasMultipleParameters(method) && !isArgumentsInheritor(componentType)) {
holder.registerProblem(attributeValue, "Multiple parameters have to be wrapped in Arguments")
}
}
@@ -214,4 +216,14 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool(
}
}
}
private fun hasMultipleParameters(method: PsiMethod): Boolean {
return method.parameterList.parameters
.filter { it ->
!InheritanceUtil.isInheritor(it.type, JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_TEST_INFO) &&
!InheritanceUtil.isInheritor(it.type, JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_TEST_REPORTER)
}
.count() > 1 && !MetaAnnotationUtil.isMetaAnnotated(method, Collections.singleton(
JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_EXTENSION_EXTEND_WITH))
}
}
@@ -35,6 +35,8 @@ public class JUnit5MalformedParameterizedTest extends LightInspectionTestCase {
"public @interface ParameterizedTest {}");
addEnvironmentClass("package org.junit.jupiter.api;\n" +
"public @interface Test {}");
addEnvironmentClass("package org.junit.jupiter.api;\n" +
"public interface TestInfo {}");
addEnvironmentClass("package org.junit.jupiter.params.provider;\n" +
"public @interface MethodSource {String[] names();}");
addEnvironmentClass("package org.junit.jupiter.params.provider;\n" +
@@ -53,6 +55,7 @@ public class JUnit5MalformedParameterizedTest extends LightInspectionTestCase {
public void testMalformedSources() { doTest(); }
public void testMalformedSourcesArguments() { doTest(); }
public void testMalformedSourcesImplicitConversion() { doTest(); }
public void testMalformedSourcesImplicitParameters() { doTest(); }
@Override
protected String getBasePath() {
@@ -0,0 +1,10 @@
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class MyJunit5 {
@ParameterizedTest
@ValueSource(strings = "foo")
void testWithRegularParameterResolver(String argument, TestInfo testReporter) {
}
}