本文へ移動
cccskills
無料GitHub で公開日本語紹介

writing-tests

IntelliJコードベースのJUnit 5テストを、適切なモジュールやフィクスチャ、ライフサイクル、UIスレッドの扱いに沿って作成・見直します。

原文原文の説明を見る

Write IntelliJ JUnit 5 tests with fixtures, lifecycle, and EDT.

JetBrains/intellij-communityスター 20,664(GitHub 20,664 + cccskills 0)

実行スコア 未検証

インストール方法を見る

こんなときに便利

  • IntelliJ向けJUnit 5テストを追加したいとき
  • テスト用フィクスチャを設定したいとき
  • UIスレッドとコルーチンの使い方を見直したいとき

日本語での紹介

できること

IntelliJコードベース向けのJUnit 5テストを作成・レビューします。テスト対象の本番モジュールに対応するテストモジュールを選び、@TestApplicationやプロジェクトフィクスチャ、@TestDisposableなどを使った構成を案内します。セットアップや後片付けにはJUnit 5のライフサイクル注釈を使い、コルーチンのテストではtimeoutRunBlockingを境界にします。UI操作だけを適切なディスパッチャ上で実行する方法や、待ち時間ではなく完了通知を使う考え方も扱います。

こんなときに便利

IntelliJのモジュールにテストを追加したいときや、既存テストのフィクスチャ、リソース解放、UIスレッド利用を見直したいときに役立ちます。JUnit 4形式からJUnit 5へ移行する場合や、コルーチンを含むテストを書く場合にも向いています。

使い方の例

  • 「この機能のテストを置くべきモジュールを確認してください」
  • 「JUnit 5でプロジェクトフィクスチャを使うテストを書いてください」
  • 「このテストのUIスレッドとコルーチンの扱いをレビューしてください」

注意点

対象はIntelliJコードベースのテストです。コマンドラインでの実行例として./tests.cmd --module <test-module> --test com.example.MyTestが記載されています。

この紹介文は、公開されている SKILL.md をもとに AI(OpenAI Codex)が作成しました。正確な仕様は下の原文を確認してください。

含まれるファイル(1)

  • SKILL.md6.8 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

<!-- Generated by community/.ai/render-guides.mjs; edit community/.agents/skills/writing-tests/SKILL.md -->

Writing Tests

Guidelines for writing tests in IntelliJ IDEA codebase.

For examples, see community/platform/testFramework/junit5/test/showcase/.

Place Tests in the Owning Module

Put a test in the test module associated with the production module it exercises. Do not place it in a downstream module merely because that module has the production module on its test classpath. Check the production module's .iml file and neighboring tests before adding a new test.

In particular, code in org.jetbrains.intellij.build.io under community/build/tasks belongs to intellij.idea.community.build.tasks.tests (community/build/tasks/test), not intellij.platform.buildScripts.tests. The BUILD_SCRIPTS_PLATFORM_TESTS group deliberately excludes org.jetbrains.intellij.build.io.* to avoid matching build-task tests by class name across module boundaries. Putting such a test in the build-scripts test module leaves it outside every community test group and causes UltimateProjectTestsStructureTest to fail.

Prefer JUnit 5 over JUnit 4

Use JUnit 5 with @TestApplication annotation instead of extending LightJavaCodeInsightFixtureTestCase.

Why JUnit 5:

  • Faster: No class hierarchy overhead, shared fixtures via companion objects
  • Cleaner: Annotations (@TestDisposable, @RegistryKey) instead of manual setup/teardown
  • Flexible: Mix EDT and non-EDT tests in one class, parameterized tests, nested tests
  • Better isolation: Each test gets fresh disposables automatically

Shared Fixtures Pattern

Use 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()
}

Lifecycle Hooks

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.

Test Disposables

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) { ... }

Registry Values in Tests

Use @RegistryKey annotation instead of Registry.get().setValue():

@Test
@RegistryKey(key = "my.registry.key", value = "true")
fun testWithRegistryEnabled() { ... }

System Properties in Tests

Use @SystemProperty annotation instead of System.setProperty():

@Test
@SystemProperty(propertyKey = "my.property", propertyValue = "value")
fun testWithSystemProperty() { ... }

Coroutines and UI Tests

Use com.intellij.testFramework.common.timeoutRunBlocking as the coroutine boundary. It has a 10-second default timeout, so a suspended test fails quickly with a coroutine-aware thread dump. Do not add a JUnit @Timeout to such a test. The two timeouts duplicate each other, and the @Timeout gives a worse diagnostic.

Keep setup, background work, and assertions off the UI thread. Move only Swing operations into a small withContext(Dispatchers.UI) block:

@Test
fun updatesLabel(): Unit = timeoutRunBlocking {
  val value = loadValue()
  val actual = withContext(Dispatchers.UI) {
    label.text = value
    label.text
  }
  assertThat(actual).isEqualTo(value)
}

Dispatchers.UI is strict: it supplies UI-thread affinity without implicit model access. Use Dispatchers.EDT only when the tested operation genuinely requires model or lock access and cannot be split from the UI operation. Keep write actions explicit.

Editor creation/disposal, editor document mutation, completion invocation, action-group expansion, and file-editor operations are common model-backed exceptions. Keep their full synchronous lifecycle in a narrow Dispatchers.EDT block; do not construct on strict UI and dispose later from a different dispatcher.

Do not use @RunInEdt, @RunMethodInEdt, runInEdtAndWait, or runInEdtAndGet in new tests. They hide the coroutine boundary, move lifecycle methods and assertions onto EDT, and can accidentally grant model or write-intent access.

When a synchronous callback API cannot call a suspending function, use a narrow bounded adapter around that callback only:

timeoutRunBlocking(timeout = 10.seconds, context = Dispatchers.UI) {
  createSwingComponent()
}

Prefer observable completion signals, flows, latches, or virtual time over sleeps. If polling is unavoidable, bound it and make the predicate suspending so UI checks can use withContext(Dispatchers.UI) without nested blocking.

Use the enclosing test scope for launched work: pass this from timeoutRunBlocking/coroutineScope, or backgroundScope from runTest, into the code under test. Create a standalone CoroutineScope(...) only when independent scope lifetime is itself under test; parent it to the test job and cancel it in finally. Use delay freely with runTest virtual time, but do not use a real timing-only delay as a completion signal.

Key Classes

  • com.intellij.testFramework.junit5.TestApplication - initializes shared application
  • com.intellij.testFramework.junit5.TestDisposable - injects test disposables
  • com.intellij.testFramework.junit5.RegistryKey - sets registry values
  • com.intellij.testFramework.junit5.SystemProperty - sets system properties
  • com.intellij.testFramework.common.timeoutRunBlocking - runs suspending test code with a 10-second default timeout
  • com.intellij.testFramework.junit5.fixture.projectFixture - creates project fixtures
  • com.intellij.testFramework.junit5.fixture.moduleFixture - creates module fixtures

Showcase Tests

  • JUnit5ProjectFixtureTest.kt - project fixture patterns
  • JUnit5DisposableTest.kt - disposable injection
  • JUnit5SystemPropertyTest.kt - system property usage
  • JUnit5RunInEdtTest.java - legacy EDT extension behavior; do not copy it for new tests

Running Tests

To run tests via command line, see TESTING.md.

Quick example:

./tests.cmd --module <test-module> --test com.example.MyTest

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

コメント

ログインなしで書き込めます。使ってみた感想、質問、動かなかった環境などを気軽にどうぞ。

まだコメントはありません。最初のコメントを書いてみませんか。

送信するとすぐに公開されます。メールアドレスや電話番号などの個人情報は書かないでください。URL は 2 つまでです。

0 / 1000 文字

このフォームは reCAPTCHA で保護されており、Google のプライバシーポリシーと利用規約が適用されます。

同じリポジトリのスキル

actions

無料概要あり

IntelliJプラグインのAnActionと登録設定を実装・変更し、表示文言やアイコンの定義、非同期処理の起動方法を整えます。

  • IDEに新しいアクションを追加したいとき
  • アクションの登録やアイコンを変更したいとき
  • 表示名と説明文の定義を整理したいとき
JetBrains/intellij-community2.1万2026年10月26日 更新

bazel-test-migration

無料概要あり

IntelliJのJPSテストモジュールをBazelへ移行し、Bazelだけで起きる失敗を依存関係や実行環境から切り分ける手順を案内します。

  • JPSテストをBazelへ移行したいとき
  • Bazelだけで起きるテスト失敗を調べたいとき
  • テストの依存関係やデータ設定を確認したいとき
JetBrains/intellij-community2.1万2026年10月26日 更新

code-style

無料概要あり

IntelliJのKotlin・Javaコードを新規作成やレビューする際に、言語の選び方、書式、型の扱い、例外処理などのプロジェクト規約に沿って整えるスキルです。

  • IntelliJ規約に沿ってKotlinを書きたいとき
  • Javaコードをレビューしたいとき
  • 例外処理やsealed型の分岐を確認したいとき
JetBrains/intellij-community2.1万2026年10月26日 更新

commits

無料概要あり

IntelliJ向けの変更を適切な単位に分け、チケットの有無に応じた形式で、理由や判断も伝わるコミットメッセージを作成・修正するスキルです。

  • チケット付き変更のメッセージを作りたいとき
  • テストやドキュメントのコミットを整理したいとき
  • 既存のコミットメッセージを修正したいとき
JetBrains/intellij-community2.1万2026年10月26日 更新

compare-python-typecheckers

無料概要あり

Pythonコードの型推論や型エラーを複数の型チェッカーで比較し、各ツールの判定と診断内容をまとめたレポートを作成します。

  • 型推論の違いを比較したいとき
  • 型エラーになるか確かめたいとき
  • 複数チェッカーの診断を見比べたいとき
JetBrains/intellij-community2.1万2026年10月26日 更新

compose-ui-architecture

無料概要あり

Compose Desktopの画面で状態や副作用をどこに持たせるか整理し、表示と判断の境界、再利用部品やリストの設計を整えます。

  • 画面とプレゼンターの責務を分けたいとき
  • UI状態の置き場所を整理したいとき
  • Effectやコルーチンの使い方を見直したいとき
JetBrains/intellij-community2.1万2026年10月26日 更新

JetBrains のスキルをすべて見る

このスキルの問題を報告する