junit 5: warn if @Test is used together with @ParameterizedTest

This commit is contained in:
Anna.Kozlova
2017-04-24 17:24:13 +02:00
parent 26861057f4
commit cee3750a30
4 changed files with 15 additions and 0 deletions
@@ -35,4 +35,5 @@ public class JUnitCommonClassNames {
public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_CSV_FILE_SOURCE = "org.junit.jupiter.params.provider.CsvFileSource";
public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_ARGUMENTS_SOURCE = "org.junit.jupiter.params.provider.ArgumentsSource";
public static final String ORG_JUNIT_JUPITER_PARAMS_CONVERTER_CONVERT_WITH = "org.junit.jupiter.params.converter.ConvertWith";
public static final String ORG_JUNIT_JUPITER_API_TEST = "org.junit.jupiter.api.Test";
}
@@ -17,6 +17,7 @@ package com.intellij.execution.junit.codeInsight
import com.intellij.codeInsight.AnnotationUtil
import com.intellij.codeInsight.daemon.impl.analysis.JavaGenericsUtil
import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix
import com.intellij.codeInsight.intention.QuickFixFactory
import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool
import com.intellij.codeInspection.ProblemHighlightType
@@ -53,6 +54,12 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool(
val modifierList = method.modifierList
val parameterizedAnnotation = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PARAMETERIZED_TEST)
if (parameterizedAnnotation != null) {
val testAnnotation = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_TEST)
if (testAnnotation != null && method.parameterList.parametersCount > 0) {
holder.registerProblem(testAnnotation,
"Suspicious combination @Test and @ParameterizedTest",
DeleteElementFix(testAnnotation))
}
val methodSource = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PROVIDER_METHOD_SOURCE)
if (methodSource != null) {
checkMethodSource(method, methodSource)
@@ -33,6 +33,8 @@ public class JUnit5MalformedParameterizedTest extends LightInspectionTestCase {
super.setUp();
addEnvironmentClass("package org.junit.jupiter.params;\n" +
"public @interface ParameterizedTest {}");
addEnvironmentClass("package org.junit.jupiter.api;\n" +
"public @interface Test {}");
addEnvironmentClass("package org.junit.jupiter.params.provider;\n" +
"public @interface MethodSource {String[] names();}");
addEnvironmentClass("package org.junit.jupiter.params.provider;\n" +
@@ -63,4 +63,9 @@ class ParameterizedTestsDemo {
<warning descr="Multiple parameters are not supported by this source">@ValueSource(ints = 1)</warning>
void testWithValuesMultipleParams(int i, int j) { }
@ParameterizedTest
@ValueSource(ints = {1})
<warning descr="Suspicious combination @Test and @ParameterizedTest">@org.junit.jupiter.api.Test</warning>
void testWithTestAnnotation(int i) { }
}