はじめに
ついにUnitテストと真面目に向き合う時がやってきたので、採択技術や書き方をメモしておく。
採択したもの
- JUnit4
- Mockk
- Truth(Googleが出しているアサーションライブラリ)
↓kts化してない人は読み替えてください
build.gradle.kts(app)
// test
testImplementation("junit:junit:4.13.2")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.8.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
testImplementation("io.mockk:mockk:1.12.5")
testImplementation("com.google.truth:truth:1.1.3")
testImplementation("androidx.arch.core:core-testing:2.1.0")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1")
なぜそうしたのか
- Mockk : Coroutineをサポートしているライブラリだから。
- coEvery{} と書くとCoroutineによる処理の実行がモックできる。
- Truth : 値の比較をするための鉄板ライブラリ。
- JUnit4 : 一旦慣れているものを使う方向で。追々JUnit5に移行したい。
参考にした本、サイト
Android ユニットテスト ヒッチハイク・ガイド | 糖衣構文
https://syntaxsugar.booth.pm/items/4186246
【Android/Kotlin】JUnit4 によるユニットテストを試してみた
https://zenn.dev/kotaro666/articles/android-unit-test-sample
ahmedeltaher/MVVM-Kotlin-Android-Architecture
https://github.com/ahmedeltaher/MVVM-Kotlin-Android-Architecture