[Java. Tests] IDEA-381948 Introduce better API when running JUnit5 tests

GitOrigin-RevId: e87f97a3a11cdf4e3f11c204953604a7b380aa6a
This commit is contained in:
Georgii Ustinov
2025-11-12 20:12:33 +00:00
committed by intellij-monorepo-bot
parent cf5bccb087
commit e9e15deeba
4 changed files with 62 additions and 30 deletions
@@ -1,19 +1,15 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.migration
import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.roots.ModuleRootModificationUtil
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.testFramework.IdeaTestUtil
import com.intellij.testFramework.IndexingTestUtil
import com.intellij.pom.java.LanguageLevel
import com.intellij.testFramework.JavaJUnit5Util.javaCodeInsightFixture
import com.intellij.testFramework.JavaJUnit5Util.setUpJdk
import com.intellij.testFramework.TestDataPath
import com.intellij.testFramework.junit5.RunInEdt
import com.intellij.testFramework.junit5.RunMethodInEdt
import com.intellij.testFramework.junit5.TestApplication
import com.intellij.testFramework.junit5.fixture.*
import com.intellij.testFramework.junit5.javaCodeInsightFixture
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
@@ -29,13 +25,7 @@ class TryFinallyCanBeTryWithResourcesInspectionTest {
@JvmStatic
@RunMethodInEdt
fun beforeAll() {
val jdk = IdeaTestUtil.getMockJdk9()
WriteAction.runAndWait<Exception> {
ProjectJdkTable.getInstance().addJdk(jdk, disposable)
ProjectRootManager.getInstance(project.get()).setProjectSdk(jdk)
ModuleRootModificationUtil.setModuleSdk(module, jdk)
}
IndexingTestUtil.waitUntilIndexesAreReady(project.get())
setUpJdk(LanguageLevel.JDK_1_9, project.get(), module, disposable)
}
private val disposable by disposableFixture()
@@ -107,6 +107,13 @@ public final class IdeaTestUtil {
});
}
/**
* Returns a mock JDK for the specified language level.
*/
public static @NotNull Sdk getMockJdk(@NotNull LanguageLevel level) {
return getMockJdk(level.toJavaVersion());
}
public static @NotNull Sdk getMockJdk(@NotNull JavaVersion version) {
int mockJdk = version.feature >= 25 ? 25 :
version.feature >= 21 ? 21 :
@@ -0,0 +1,51 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.testFramework
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.roots.ModuleRootModificationUtil
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.platform.testFramework.junit5.codeInsight.fixture.codeInsightFixture
import com.intellij.pom.java.LanguageLevel
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import com.intellij.testFramework.fixtures.impl.JavaCodeInsightTestFixtureImpl
import com.intellij.testFramework.junit5.fixture.TestFixture
import com.intellij.util.concurrency.annotations.RequiresEdt
import org.jetbrains.annotations.TestOnly
import java.nio.file.Path
object JavaJUnit5Util {
/**
* Creates [JavaCodeInsightTestFixture] fixture that can be used when running JUnit5 tests.
*/
@JvmStatic
@TestOnly
fun javaCodeInsightFixture(
projectFixture: TestFixture<Project>,
tempDirFixture: TestFixture<Path>,
): TestFixture<JavaCodeInsightTestFixture> = codeInsightFixture(projectFixture, tempDirFixture, ::JavaCodeInsightTestFixtureImpl)
/**
* Configures jdk for the project and the module inside JUnit5 test.
* This method should be called inside method annotated with [org.junit.jupiter.api.BeforeAll].
*
* @param level version of the JDK to be set
*/
@JvmStatic
@TestOnly
@RequiresEdt
fun setUpJdk(level: LanguageLevel, project: Project, module: Module, disposable: Disposable) {
IdeaTestUtil.getMockJdk(level)
val jdk = IdeaTestUtil.getMockJdk9()
WriteAction.runAndWait<Exception> {
ProjectJdkTable.getInstance().addJdk(jdk, disposable)
ProjectRootManager.getInstance(project).setProjectSdk(jdk)
ModuleRootModificationUtil.setModuleSdk(module, jdk)
}
IndexingTestUtil.waitUntilIndexesAreReady(project)
}
}
@@ -1,16 +0,0 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.testFramework.junit5
import com.intellij.openapi.project.Project
import com.intellij.platform.testFramework.junit5.codeInsight.fixture.codeInsightFixture
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import com.intellij.testFramework.fixtures.impl.JavaCodeInsightTestFixtureImpl
import com.intellij.testFramework.junit5.fixture.TestFixture
import org.jetbrains.annotations.TestOnly
import java.nio.file.Path
@TestOnly
fun javaCodeInsightFixture(
projectFixture: TestFixture<Project>,
tempDirFixture: TestFixture<Path>,
): TestFixture<JavaCodeInsightTestFixture> = codeInsightFixture(projectFixture, tempDirFixture, ::JavaCodeInsightTestFixtureImpl)