still treat methods with @Test annotation inside parameterized test as test method

This commit is contained in:
Anna.Kozlova
2016-10-26 10:11:08 +02:00
parent f6dac9c3e7
commit 08c4fb0688
3 changed files with 57 additions and 18 deletions
@@ -58,6 +58,7 @@ public class JUnitUtil {
public static final String AFTER_EACH_ANNOTATION_NAME = "org.junit.jupiter.api.AfterEach";
public static final String PARAMETRIZED_PARAMETERS_ANNOTATION_NAME = "org.junit.runners.Parameterized.Parameters";
public static final String PARAMETRIZED_PARAMETER_ANNOTATION_NAME = "org.junit.runners.Parameterized.Parameter";
public static final String AFTER_CLASS_ANNOTATION_NAME = "org.junit.AfterClass";
public static final String BEFORE_CLASS_ANNOTATION_NAME = "org.junit.BeforeClass";
@@ -242,19 +243,6 @@ public class JUnitUtil {
public static boolean isTestAnnotated(final PsiMethod method) {
if (AnnotationUtil.isAnnotated(method, TEST_ANNOTATION, false) || JUnitRecognizer.willBeAnnotatedAfterCompilation(method)) {
final PsiAnnotation annotation = AnnotationUtil.findAnnotationInHierarchy(method.getContainingClass(), Collections.singleton(RUN_WITH));
if (annotation != null) {
final PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
for (PsiNameValuePair attribute : attributes) {
final PsiAnnotationMemberValue value = attribute.getValue();
if (value instanceof PsiClassObjectAccessExpression ) {
final PsiTypeElement typeElement = ((PsiClassObjectAccessExpression)value).getOperand();
if (typeElement.getType().getCanonicalText().equals(PARAMETERIZED_CLASS_NAME)) {
return false;
}
}
}
}
return true;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,16 +20,14 @@
*/
package com.intellij.execution.junit2.inspection;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInspection.reference.EntryPoint;
import com.intellij.codeInspection.reference.RefElement;
import com.intellij.execution.junit.JUnitUtil;
import com.intellij.openapi.util.DefaultJDOMExternalizer;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.*;
import com.intellij.psi.search.searches.ClassInheritorsSearch;
import com.intellij.psi.util.PsiClassUtil;
import com.intellij.psi.util.PsiTreeUtil;
@@ -78,6 +76,9 @@ public class JUnitEntryPoint extends EntryPoint {
}
if (JUnitUtil.isTestMethodOrConfig(method)) return true;
}
else if (psiElement instanceof PsiField) {
return AnnotationUtil.isAnnotated((PsiField)psiElement, JUnitUtil.PARAMETRIZED_PARAMETER_ANNOTATION_NAME, false);
}
return false;
}
@@ -0,0 +1,50 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.execution.junit;
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
public class JUnitUnusedMethodsTest extends LightCodeInsightFixtureTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.addClass("package org.junit; public @interface Test {}");
myFixture.addClass("package org.junit.runners; public class Parameterized { public @interface Parameters {} public @interface Parameter {}}");
myFixture.addClass("package org.junit.runner; public @interface RunWith {Class value();}");
myFixture.enableInspections(new UnusedDeclarationInspection(true));
}
public void testRecognizeTestMethodInParameterizedClass() throws Exception {
myFixture.configureByText("A.java", "import org.junit.Test;\n" +
"import org.junit.runner.RunWith;\n" +
"import org.junit.runners.Parameterized;\n" +
"import java.util.*;\n" +
"@RunWith(Parameterized.class)\n" +
"public class A {\n" +
" @Parameterized.Parameters\n" +
" public static Collection<Object[]> data() {\n" +
" return Arrays.asList(new Object[] {\"11\"}, new Object[] {\"12\"});\n" +
" }\n" +
" @Parameterized.Parameter\n" +
" public String myJUnitVersion;\n" +
" @Test\n" +
" public void ignoredTestMethod() throws Exception {}\n" +
"}\n");
myFixture.testHighlighting(true, false, false);
}
}