Guidelines for writing tests in IntelliJ codebase. Use when creating new test classes or test methods.
Guidelines for writing tests in IntelliJ IDEA codebase.
For examples, see community/platform/testFramework/junit5/test/showcase/.
Use JUnit 5 with @TestApplication annotation instead of extending LightJavaCodeInsightFixtureTestCase.
Why JUnit 5:
@TestDisposable, @RegistryKey) instead of manual setup/teardownUse companion object fixtures shared between all tests:
@TestApplication
internal class MyTest {
companion object {
private val projectFixture = projectFixture()
private val moduleFixture = projectFixture.moduleFixture("src")
}
private val project get() = projectFixture.get()
private val module get() = moduleFixture.get()
}
Use JUnit 5 lifecycle annotations for setup and teardown:
@TestApplication
internal class MyTest {
companion object {
@JvmStatic
@BeforeAll
fun setUpClass() {
// Once before all tests in class
}
@JvmStatic
@AfterAll
fun tearDownClass() {
// Once after all tests in class
}
}
@BeforeEach
fun setUp() {
// Before each test method
}
@AfterEach
fun tearDown() {
// After each test method
}
}
Note: Prefer @TestDisposable over manual @AfterEach cleanup for resources.
Use @TestDisposable annotation to inject test-scoped disposables (created before each test, disposed after):
@TestDisposable
lateinit var disposable: Disposable
// Or as parameter
@Test
fun myTest(@TestDisposable disposable: Disposable) { ... }
Use @RegistryKey annotation instead of Registry.get().setValue():
@Test
@RegistryKey(key = "my.registry.key", value = "true")
fun testWithRegistryEnabled() { ... }
Use @SystemProperty annotation instead of System.setProperty():
@Test
@SystemProperty(propertyKey = "my.property", propertyValue = "value")
fun testWithSystemProperty() { ... }
Use @RunInEdt annotation to run test methods in EDT:
@TestApplication
@RunInEdt
class MyEdtTest {
@Test
fun testInEdt() { ... }
}
// Or for specific methods only
@TestApplication
@RunInEdt(allMethods = false)
class MyTest {
@Test
@RunMethodInEdt
fun testInEdt() { ... }
}
com.intellij.testFramework.junit5.TestApplication - initializes shared applicationcom.intellij.testFramework.junit5.TestDisposable - injects test disposablescom.intellij.testFramework.junit5.RegistryKey - sets registry valuescom.intellij.testFramework.junit5.SystemProperty - sets system propertiescom.intellij.testFramework.junit5.RunInEdt - runs tests in EDTcom.intellij.testFramework.junit5.fixture.projectFixture - creates project fixturescom.intellij.testFramework.junit5.fixture.moduleFixture - creates module fixturesJUnit5ProjectFixtureTest.kt - project fixture patternsJUnit5DisposableTest.kt - disposable injectionJUnit5SystemPropertyTest.kt - system property usageJUnit5RunInEdtTest.java - EDT executionTo run tests via command line, see TESTING.md.
Quick example:
./tests.cmd -Dintellij.build.test.patterns=*MyTest