diff --git a/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java b/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java index 4f1846b82b4e..4cff4bd834ea 100644 --- a/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java +++ b/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java @@ -64,9 +64,13 @@ public class JUnitUtil { public static final String AFTER_CLASS_ANNOTATION_NAME = "org.junit.AfterClass"; public static final String BEFORE_CLASS_ANNOTATION_NAME = "org.junit.BeforeClass"; + public static final Collection TEST5_CONFIG_METHODS = Collections.unmodifiableList(Arrays.asList(BEFORE_EACH_ANNOTATION_NAME, + AFTER_EACH_ANNOTATION_NAME)); public static final String BEFORE_ALL_ANNOTATION_NAME = "org.junit.jupiter.api.BeforeAll"; public static final String AFTER_ALL_ANNOTATION_NAME = "org.junit.jupiter.api.AfterAll"; + public static final Collection TEST5_STATIC_CONFIG_METHODS = Collections.unmodifiableList(Arrays.asList(BEFORE_ALL_ANNOTATION_NAME, + AFTER_ALL_ANNOTATION_NAME)); private static final Collection TEST_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST_ANNOTATION, TEST5_ANNOTATION, diff --git a/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedRepeatedTestInspection.kt b/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedRepeatedTestInspection.kt index 1794984b4b7e..9d1fcade2800 100644 --- a/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedRepeatedTestInspection.kt +++ b/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedRepeatedTestInspection.kt @@ -15,6 +15,7 @@ */ package com.intellij.execution.junit.codeInsight +import com.intellij.codeInsight.MetaAnnotationUtil import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool import com.intellij.codeInspection.ProblemsHolder @@ -48,7 +49,7 @@ class JUnit5MalformedRepeatedTestInspection : BaseJavaBatchLocalInspectionTool() val modifierList = method.modifierList val repeatedAnno = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_REPEATED_TEST) if (repeatedAnno != null) { - val testAnno = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_TEST) + val testAnno = MetaAnnotationUtil.findMetaAnnotations(method, JUnitUtil.TEST5_ANNOTATIONS).findFirst().orElse(null) if (testAnno != null) { holder.registerProblem(testAnno, "Suspicious combination @Test and @RepeatedTest", DeleteElementFix(testAnno)) @@ -66,21 +67,18 @@ class JUnit5MalformedRepeatedTestInspection : BaseJavaBatchLocalInspectionTool() val repetitionType = JavaPsiFacade.getElementFactory(holder.project).createType(repetitionInfo!!) val repetitionInfoParam = method.parameterList.parameters.find { it.type.isAssignableFrom(repetitionType) } if (repetitionInfoParam != null) { - val testAnno = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_TEST) - if (testAnno != null) { + if (MetaAnnotationUtil.isMetaAnnotated(method, JUnitUtil.TEST5_ANNOTATIONS)) { holder.registerProblem(repetitionInfoParam.nameIdentifier ?: repetitionInfoParam, "RepetitionInfo is injected for @RepeatedTest only") } else { - val beforeAll = modifierList.findAnnotation(JUnitUtil.BEFORE_ALL_ANNOTATION_NAME) - val afterAll = modifierList.findAnnotation(JUnitUtil.AFTER_ALL_ANNOTATION_NAME) - if (beforeAll != null || afterAll != null) { + val anno = MetaAnnotationUtil.findMetaAnnotations(method, JUnitUtil.TEST5_STATIC_CONFIG_METHODS).findFirst().orElse(null) + if (anno != null) { + val qName = anno.qualifiedName holder.registerProblem(repetitionInfoParam.nameIdentifier ?: repetitionInfoParam, - "RepetitionInfo is injected for @BeforeEach/@AfterEach only, but not for " + StringUtil.getShortName((beforeAll ?: afterAll)!!.qualifiedName!!)) + "RepetitionInfo is injected for @BeforeEach/@AfterEach only, but not for " + StringUtil.getShortName(qName!!)) } else { - val beforeEach = modifierList.findAnnotation(JUnitUtil.BEFORE_EACH_ANNOTATION_NAME) - val afterEach = modifierList.findAnnotation(JUnitUtil.AFTER_EACH_ANNOTATION_NAME) - if ((beforeEach != null || afterEach != null) && method.containingClass?.methods?.find { it.modifierList.findAnnotation(JUnitUtil.TEST5_ANNOTATION) != null} != null) { + if (MetaAnnotationUtil.isMetaAnnotated(method, JUnitUtil.TEST5_CONFIG_METHODS) && method.containingClass?.methods?.find { MetaAnnotationUtil.isMetaAnnotated(it, JUnitUtil.TEST5_ANNOTATIONS)} != null) { holder.registerProblem(repetitionInfoParam.nameIdentifier ?: repetitionInfoParam, "RepetitionInfo won't be injected for @Test methods") }