Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/okuzawats/android-hilt-testing
Hiltを単体テストに活用するための実験用プロジェクト。
https://github.com/okuzawats/android-hilt-testing
android hilt testing
Last synced: 3 days ago
JSON representation
Hiltを単体テストに活用するための実験用プロジェクト。
- Host: GitHub
- URL: https://github.com/okuzawats/android-hilt-testing
- Owner: okuzawats
- Created: 2023-06-29T05:50:55.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-08T01:53:00.000Z (about 1 year ago)
- Last Synced: 2025-02-07T00:42:50.669Z (8 days ago)
- Topics: android, hilt, testing
- Language: Kotlin
- Homepage:
- Size: 154 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Android Hilt Testing
Hiltを単体テストに活用するための実験用プロジェクト。
## setup Hilt
通常通りにHiltをセットアップ
## setup test
テスト用にHiltの依存、TestRunner、Robolectricを追加
```groovy
// use Hilt in tests
testImplementation 'com.google.dagger:hilt-android-testing:2.46.1'
kaptTest 'com.google.dagger:hilt-android-compiler:2.46.1'
// TestRunner
testImplementation 'androidx.test.ext:junit:1.1.5'
// Robolectric
testImplementation 'org.robolectric:robolectric:4.10.3'
```## JUnit4
`@Config` に `HiltTestApplication::class` を、`@RunWith` に `AndroidJUnit4::class)` を指定する。
テストルールとして `HiltAndroidRule` を適用した上で、setup内で `inject` を呼び出す。
Robolectricの仕組みを利用してConfigを設定するため、Robolectricが必要になる。
RunnerはAndroidJUnit4ではなくRobolectricTestRunnerでも良い。```kotlin
package com.okuzawats.android.hilt.testingimport androidx.test.ext.junit.runners.AndroidJUnit4
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import dagger.hilt.android.testing.HiltTestApplication
import org.amshove.kluent.shouldBe
import org.junit.Testimport org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import javax.inject.Inject/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@HiltAndroidTest
@Config(application = HiltTestApplication::class)
@RunWith(AndroidJUnit4::class)
class ExampleUnitTest {@get:Rule
var hiltRule = HiltAndroidRule(this)@Inject lateinit var animal: Animal
@Before
fun setup() {
hiltRule.inject()
}@Test
fun addition_isCorrect() {
// Productionは42を返すが、テスト用は100を返す
animal.bow() shouldBe 100
// animal.bow() shouldBe 42
}
}
```## Module
`test` 内でHiltによって解決される依存関係を差し替える。
`@TestInstallIn` にComponentと置き換え対象となるModuleを指定する。```kotlin
package com.okuzawats.android.hilt.testingimport dagger.Module
import dagger.Provides
import dagger.hilt.components.SingletonComponent
import dagger.hilt.testing.TestInstallIn
import io.mockk.every
import io.mockk.mockk@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [AnimalModule::class]
)
class FakeAnimalModule {
@Provides
fun provideAnimal(): Animal {
return mockk().also {
every { it.bow() } returns 100
}
}
}
```