testing-strategies について
testing-strategiesは、Micronaut、Retrofit、MockKを使うプロジェクト向けのKotlinテスト資料です。エンドポイントとリポジトリのカバレッジ、命名規則、コルーチンのモック方法、テスト実行コマンドを説明します。
主な機能
- 01エンドポイント統合テストの指針
- 02リポジトリテストのパターン
- 03テスト命名とカバレッジ要件
- 04MockKとコルーチンのテスト規則
- 05テスト実行用Gradleコマンド
活用シーン
- 01APIエンドポイントのエンドツーエンドテスト追加
- 02認証失敗や未検出時の動作確認
- 03リポジトリとコルーチンコードのテスト
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ c0x12c/ ai-toolkit --skill testing-strategiesインストール後、Claude Code で/testing-strategiesを入力して読み込まれたか確認します。
同梱ファイルexamples.mdintegration-test-setup.md
詳細
テスト戦略 — クイックリファレンス
@MicronautTestの設定、Retrofit クライアントのセットアップ、接続プールのチューニングについては、integration-test-setup.mdを参照してください。MockK、テストデータビルダー、アサーションを含む完全なコードパターンについては、
examples.mdを参照してください。
統合テスト
すべてのエンドポイントに統合テストが必要です。AbstractControllerTest を拡張し、module-client の Retrofit クライアントを使用して、@AfterEach で truncateAllTables() を呼び出してクリーンアップしてください。
主なルール:
- 必ず Retrofit クライアントを使用する — 生の
HttpRequestは使用しないでください accessToken()で実際の JWT トークンを生成する- マネージャーをモックせず、HTTP スタック全体をエンドツーエンドでテストする
テストの命名
@Test
fun `{action} - {expected outcome}`() = runBlocking { }
// Examples:
fun `getEmployee - returns employee by id`() = runBlocking { }
fun `getEmployee - returns 404 when not found`() = runBlocking { }
fun `createEmployee - fails with 401 when not authenticated`() = runBlocking { }
エンドポイントごとに必要なテストカバレッジ
- 正常系 — 基本的な成功ケース
- 未検出 — リソースが見つからない場合の 404
- 認証失敗 — トークンなしの場合の 401
- 論理削除 — 削除済みのレコードが返されないこと
@Test
fun `getById - returns 404 when not found`() = runBlocking {
val token = accessToken(prepareUser())
assertThrows<HttpException> {
runBlocking {
client.getById(authorization = token, id = UUID.randomUUID())
}
}.also {
assertThat(it.code()).isEqualTo(404)
}
}
リポジトリのテスト
AbstractRepositoryTest を拡張します。すべてのリポジトリで、最低限以下のテストが必要です。
class DefaultEmployeeRepositoryTest : AbstractRepositoryTest() {
private val repository = DefaultEmployeeRepository(database)
@BeforeEach
fun cleanup() { database.primary.truncateAllTables() }
@Test
fun `insert - happy path`() { }
@Test
fun `byId - returns entity when exists`() { }
@Test
fun `byId - returns null when not exists`() { }
@Test
fun `byId - returns null when soft deleted`() { }
@Test
fun `deleteById - soft deletes entity`() { }
@Test
fun `update - updates selected fields`() { }
}
MockK のルール
- モックの作成には
mockk<T>()を使用する - サスペンド関数には
coEvery/coVerifyを使用する(every/verifyは使用しない) - 渡された引数を検証するには、
slot<T>()とcapture()を使用する kotlinx.coroutines.testのrunTest { }を使用する —runBlockingは使用しないでください。runTestは仮想時間を処理し、コルーチンの問題を検出します。- コメント付きの AAA パターンを使用する:
// Given、// When、// Then - 関連するテストをグループ化するには、
@Nestedと@DisplayNameを使用する
MockK の完全なコード例、
@Nestedのパターン、テストデータビルダー、AssertJ アサーションについては、examples.mdを参照してください。
注意点
接続プールの枯渇: 並列テストが「cannot acquire connection」で失敗する場合は、maxPoolSize を 20 に増やしてください。設定例については、integration-test-setup.md を参照してください。
MockK とコルーチン: サスペンド関数には必ず coEvery / coVerify を使用してください。通常の every / verify は動作せず、分かりにくいエラーが発生します。
テストの実行
./gradlew test # All tests
./gradlew test --tests "com.yourcompany.EmployeeControllerTest" # One class
./gradlew :app:module-repository:test # One module