{"id":25229992,"url":"https://github.com/okuzawats/android-hilt-testing","last_synced_at":"2026-05-17T18:02:50.795Z","repository":{"id":177191832,"uuid":"660042226","full_name":"okuzawats/android-hilt-testing","owner":"okuzawats","description":"Hiltを単体テストに活用するための実験用プロジェクト。","archived":false,"fork":false,"pushed_at":"2024-02-08T01:53:00.000Z","size":158,"stargazers_count":2,"open_issues_count":11,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-13T21:40:27.717Z","etag":null,"topics":["android","hilt","testing"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/okuzawats.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-06-29T05:50:55.000Z","updated_at":"2024-04-06T13:28:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"f72f35d8-4b86-4e99-bc7f-2b4257ecba59","html_url":"https://github.com/okuzawats/android-hilt-testing","commit_stats":null,"previous_names":["okuzawats/android-hilt-testing"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/okuzawats/android-hilt-testing","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okuzawats%2Fandroid-hilt-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okuzawats%2Fandroid-hilt-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okuzawats%2Fandroid-hilt-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okuzawats%2Fandroid-hilt-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/okuzawats","download_url":"https://codeload.github.com/okuzawats/android-hilt-testing/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okuzawats%2Fandroid-hilt-testing/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265531410,"owners_count":23783231,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","hilt","testing"],"created_at":"2025-02-11T11:54:05.394Z","updated_at":"2026-05-17T18:02:50.706Z","avatar_url":"https://github.com/okuzawats.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Android Hilt Testing\n\nHiltを単体テストに活用するための実験用プロジェクト。\n\n## setup Hilt\n\n通常通りにHiltをセットアップ\n\n## setup test\n\nテスト用にHiltの依存、TestRunner、Robolectricを追加\n\n```groovy\n // use Hilt in tests\n testImplementation 'com.google.dagger:hilt-android-testing:2.46.1'\n kaptTest 'com.google.dagger:hilt-android-compiler:2.46.1'\n // TestRunner\n testImplementation 'androidx.test.ext:junit:1.1.5'\n // Robolectric\n testImplementation 'org.robolectric:robolectric:4.10.3'\n```\n\n## JUnit4\n\n`@Config` に `HiltTestApplication::class` を、`@RunWith` に `AndroidJUnit4::class)` を指定する。\nテストルールとして `HiltAndroidRule` を適用した上で、setup内で `inject` を呼び出す。\nRobolectricの仕組みを利用してConfigを設定するため、Robolectricが必要になる。\nRunnerはAndroidJUnit4ではなくRobolectricTestRunnerでも良い。\n\n```kotlin\npackage com.okuzawats.android.hilt.testing\n\nimport androidx.test.ext.junit.runners.AndroidJUnit4\nimport dagger.hilt.android.testing.HiltAndroidRule\nimport dagger.hilt.android.testing.HiltAndroidTest\nimport dagger.hilt.android.testing.HiltTestApplication\nimport org.amshove.kluent.shouldBe\nimport org.junit.Test\n\nimport org.junit.Assert.*\nimport org.junit.Before\nimport org.junit.Rule\nimport org.junit.runner.RunWith\nimport org.robolectric.annotation.Config\nimport javax.inject.Inject\n\n/**\n * Example local unit test, which will execute on the development machine (host).\n *\n * See [testing documentation](http://d.android.com/tools/testing).\n */\n@HiltAndroidTest\n@Config(application = HiltTestApplication::class)\n@RunWith(AndroidJUnit4::class)\nclass ExampleUnitTest {\n\n    @get:Rule\n    var hiltRule = HiltAndroidRule(this)\n\n    @Inject lateinit var animal: Animal\n\n    @Before\n    fun setup() {\n        hiltRule.inject()\n    }\n\n    @Test\n    fun addition_isCorrect() {\n        // Productionは42を返すが、テスト用は100を返す\n        animal.bow() shouldBe 100\n        // animal.bow() shouldBe 42\n    }\n}\n```\n\n## Module\n\n`test` 内でHiltによって解決される依存関係を差し替える。\n`@TestInstallIn` にComponentと置き換え対象となるModuleを指定する。\n\n```kotlin\npackage com.okuzawats.android.hilt.testing\n\nimport dagger.Module\nimport dagger.Provides\nimport dagger.hilt.components.SingletonComponent\nimport dagger.hilt.testing.TestInstallIn\nimport io.mockk.every\nimport io.mockk.mockk\n\n@Module\n@TestInstallIn(\n    components = [SingletonComponent::class],\n    replaces = [AnimalModule::class]\n)\nclass FakeAnimalModule {\n    @Provides\n    fun provideAnimal(): Animal {\n        return mockk\u003cAnimal\u003e().also {\n            every { it.bow() } returns 100\n        }\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fokuzawats%2Fandroid-hilt-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fokuzawats%2Fandroid-hilt-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fokuzawats%2Fandroid-hilt-testing/lists"}