1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Spring Boot アプリにテストコードを書く方法

1
Posted at

Spring Boot アプリにテストコードを書く方法についてまとめていきます。

テストコードを書く優先順位

  1. Service
  2. Validation(自作)
  3. Filter(認証)
  4. Repository
  5. Controller
  6. Integration
  7. utils
  8. enum / Config(必要なら)

1 から 3 はユニットテスト、その次にスライステスト(Repository, Controller)、最後に結合テストを書いて、必要なら utils や enum, Config のテストも書いていきます。

utils のテスト

1) ロジックありの場合

例:

  • 日付計算
  • 月初・月末算出
  • 金額丸め
  • 集計ロジック

Service から切り出したロジックは単体テスト必須です。

2) 単なる変換 utils

例:

  • Entity → DTO 変換
  • Optional ラップ
  • null チェック

これらは Service テスト or Controller テストで間接的にカバーされるため、基本いりません。

単体テストを書いても「実装とテストが同じ」になりがちです。

3) 定数・ラッパー系(いらない)

例:

  • 定数クラス
  • String フォーマット
  • UUID 生成の薄いラッパー

テスト価値ほぼゼロです。

enum のテスト

ロジックがある enum はテスト対象です。ただの定数クラスは不要。

Config のテスト

書かなくていい場合

  • Bean 定義だけ
  • プロパティバインド

書くべき

  • Security 設定
  • CORS
  • Jackson 設定

多くは Integration Test で確認します。

その他

DTO / Entity / exception は 基本スキップです。

例外的に Entity で equals / hashCode をカスタムしてる場合は書くこともあります。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?