[junit] Ignore before/after suite in assertions check

#IDEA-381495 Fixed


(cherry picked from commit 8851f6f0f2e2e095c3e2fe7b3c7ce96b6791ff74)

IJ-CR-181127

GitOrigin-RevId: 3ea3e04973c94fd570ec73b6411ed9bc56dfce46
This commit is contained in:
Bart van Helvert
2025-10-31 17:25:13 +01:00
committed by intellij-monorepo-bot
parent 8a5be64633
commit 4187fd2ce3
5 changed files with 30 additions and 2 deletions

View File

@@ -24,6 +24,7 @@ import com.intellij.testIntegration.JavaTestFramework;
import com.intellij.testIntegration.TestFramework;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.ContainerUtil;
import com.siyeh.ig.junit.JUnitCommonClassNames;
import com.siyeh.ig.psiutils.TestUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -76,7 +77,8 @@ public final class JUnitUtil {
private static final Collection<String> CONFIGURATIONS_ANNOTATION_NAME =
List.of(DATA_POINT, AFTER_ANNOTATION_NAME, BEFORE_ANNOTATION_NAME, AFTER_EACH_ANNOTATION_NAME, BEFORE_EACH_ANNOTATION_NAME,
AFTER_CLASS_ANNOTATION_NAME, BEFORE_CLASS_ANNOTATION_NAME, BEFORE_ALL_ANNOTATION_NAME, AFTER_ALL_ANNOTATION_NAME, RULE_ANNOTATION);
AFTER_CLASS_ANNOTATION_NAME, BEFORE_CLASS_ANNOTATION_NAME, BEFORE_ALL_ANNOTATION_NAME, AFTER_ALL_ANNOTATION_NAME, RULE_ANNOTATION,
JUnitCommonClassNames.ORG_JUNIT_PLATFORM_SUITE_API_AFTERSUITE, JUnitCommonClassNames.ORG_JUNIT_PLATFORM_SUITE_API_BEFORESUITE);
public static final String PARAMETERIZED_CLASS_NAME = "org.junit.runners.Parameterized";
public static final String SUITE_CLASS_NAME = "org.junit.runners.Suite";
@@ -135,10 +137,10 @@ public final class JUnitUtil {
if (psiMethod.isConstructor()) return false;
if (psiMethod.hasModifierProperty(PsiModifier.PRIVATE)) return false;
if (isTestAnnotated(psiMethod, true)) return !psiMethod.hasModifierProperty(PsiModifier.STATIC);
if (AnnotationUtil.isAnnotated(psiMethod, CONFIGURATIONS_ANNOTATION_NAME, 0)) return false;
if (aClass != null && MetaAnnotationUtil.isMetaAnnotatedInHierarchy(aClass, Collections.singletonList(CUSTOM_TESTABLE_ANNOTATION))) return true;
if (!psiMethod.hasModifierProperty(PsiModifier.PUBLIC)) return false;
if (psiMethod.hasModifierProperty(PsiModifier.ABSTRACT)) return false;
if (AnnotationUtil.isAnnotated(psiMethod, CONFIGURATIONS_ANNOTATION_NAME, 0)) return false;
if (checkClass && checkRunWith) {
PsiAnnotation annotation = getRunWithAnnotation(aClass);
if (annotation != null) {

View File

@@ -85,6 +85,8 @@ public final @NonNls class JUnitCommonClassNames {
public static final String ORG_JUNIT_EXPERIMENTAL_RUNNERS_ENCLOSED = "org.junit.experimental.runners.Enclosed";
public static final String ORG_JUNIT_JUPITER_API_IO_TEMPDIR = "org.junit.jupiter.api.io.TempDir";
public static final String ORG_JUNIT_PLATFORM_SUITE_API_SUITE = "org.junit.platform.suite.api.Suite";
public static final String ORG_JUNIT_PLATFORM_SUITE_API_AFTERSUITE = "org.junit.platform.suite.api.AfterSuite";
public static final String ORG_JUNIT_PLATFORM_SUITE_API_BEFORESUITE = "org.junit.platform.suite.api.BeforeSuite";
// junit 6
public static final String ORG_JUNIT_JUPITER_API_METHOD_ORDERER_DEFAULT = "org.junit.jupiter.api.MethodOrderer.Default";
}

View File

@@ -22,6 +22,7 @@ abstract class TestMethodWithoutAssertionInspectionTestBase : JvmInspectionTestB
model.addJUnit3Library()
model.addJUnit4Library()
model.addJUnit5Library()
model.addJUnitSuiteLibrary()
model.addMockKLibrary()
model.addAssertJLibrary()
}

View File

@@ -34,6 +34,10 @@ internal fun ModifiableRootModel.addJUnit5Library(version: String = "5.9.1") {
MavenDependencyUtil.addFromMaven(this, "org.junit.jupiter:junit-jupiter-params:$version")
}
internal fun ModifiableRootModel.addJUnitSuiteLibrary(version: String = "6.0.0") {
MavenDependencyUtil.addFromMaven(this, "org.junit.platform:junit-platform-suite-api:$version")
}
internal fun ModifiableRootModel.addAssertJLibrary(version: String = "3.24.2") {
MavenDependencyUtil.addFromMaven(this, "org.assertj:assertj-core:$version")
}

View File

@@ -108,4 +108,23 @@ class JavaTestMethodWithoutAssertionInspectionTest : TestMethodWithoutAssertionI
}
""".trimIndent(), "SimpleTest")
}
fun `test no highlighting on before and after suite`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
import org.junit.platform.suite.api.AfterSuite;
import org.junit.platform.suite.api.BeforeSuite;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
@Suite
@SelectPackages("example")
class BeforeAndAfterSuiteDemo {
@BeforeSuite
static void beforeSuite() { }
@AfterSuite
static void afterSuite() { }
}
""".trimIndent(), "SimpleTest")
}
}