Spock統合
モジュール
db-tester-spock
拡張クラス
パッケージ: io.github.seijikohara.dbtester.spock.extension.DatabaseTestExtension
タイプ: アノテーション駆動型拡張(IAnnotationDrivenExtension<DatabaseTest>)
登録
Specificationクラスに@DatabaseTestを追加し、DatabaseTestSupportトレイトを実装します。
groovy
@DatabaseTest
class UserRepositorySpec extends Specification implements DatabaseTestSupport {
DataSourceRegistry dbTesterRegistry = new DataSourceRegistry()
def setupSpec() {
dbTesterRegistry.registerDefault(dataSource)
}
@DataSet
@ExpectedDataSet
def 'should create user'() {
// テスト実装
}
}DatabaseTestSupportトレイト
DatabaseTestSupportトレイトはデータベーステストのコントラクトを提供します。
| プロパティ | 型 | 必須 | 説明 |
|---|---|---|---|
dbTesterRegistry | DataSourceRegistry | Yes | データソース登録 |
dbTesterConfiguration | Configuration | No | カスタム設定(デフォルトはConfiguration.defaults()) |
設定のカスタマイズ
SpecificationでgetDbTesterConfiguration()をオーバーライドします。
groovy
@DatabaseTest
class UserRepositorySpec extends Specification implements DatabaseTestSupport {
DataSourceRegistry dbTesterRegistry = new DataSourceRegistry()
Configuration dbTesterConfiguration = Configuration.builder()
.conventions(ConventionSettings.builder()
.dataFormat(DataFormat.TSV)
.build())
.build()
def setupSpec() {
dbTesterRegistry.registerDefault(dataSource)
}
@DataSet
@ExpectedDataSet
def 'should create user'() { }
}フィーチャーメソッド命名
シナリオ名はフィーチャーメソッドから導出されます。
groovy
@DataSet
def 'should create user with email'() {
// シナリオ名: "should create user with email"
}データ駆動テスト
where:ブロックを使用したパラメータ化テストでは、Spockはイテレーション名を使用します。
groovy
@DataSet
def 'should process #status order'() {
expect:
// テスト実装
where:
status << ['PENDING', 'COMPLETED']
}シナリオ名: "should process PENDING order", "should process COMPLETED order"
関連仕様
- テストフレームワーク概要 - サポートフレームワーク一覧
- JUnit - JUnit統合
- Kotest - Kotest統合
- Spring Boot - Spring Boot自動設定
- ライフサイクル - ライフサイクルフックと実行クラス
- アノテーション - アノテーションの詳細
- 設定 - 設定オプション