Skip to content
Back to skills

Android Testing

ASecurity

Use an Android instrumentation test with both `HiltAndroidRule` and `createAndroidComposeRule`. The Hilt rule must run first, and `hiltRule.inject()` must be called before the Activity or composable resolves the injected ViewModel. Use a test Activity annotated with `@AndroidEntryPoint` when the production Activity is not suitable. ```kotlin @AndroidEntryPoint class HiltTestActivity : ComponentActivity() ``` ```kotlin @HiltAndroidTest @RunWith(AndroidJUnit4::class) class FeedScreenTest { @get...

  • 584 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 5, 2026
developmentkotlinnodetestingbackend

Security analysis

A100/100

Pro scans all 18 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill android-testing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Android Testing?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Android Testing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-android-testing-4ce06e1e/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-android-testing-4ce06e1e)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# Testing a Hilt-injected ViewModel used by a Composable

Use an Android instrumentation test with both `HiltAndroidRule` and `createAndroidComposeRule`. The Hilt rule must run first, and `hiltRule.inject()` must be called before the Activity or composable resolves the injected ViewModel. Use a test Activity annotated with `@AndroidEntryPoint` when the production Activity is not suitable.

```kotlin
@AndroidEntryPoint
class HiltTestActivity : ComponentActivity()
```

```kotlin
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class FeedScreenTest {
    @get:Rule(order = 0)
    val hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 1)
    val composeRule = createAndroidComposeRule<HiltTestActivity>()

    @Before
    fun setUp() {
        hiltRule.inject()
    }

    @Test
    fun `screen renders the ViewModel state`() {
        composeRule.setContent {
            AppTheme {
                FeedRoute() // FeedRoute obtains its ViewModel with hiltViewModel()
            }
        }

        composeRule.onNodeWithTag("feed_content").assertIsDisplayed()
    }
}
```

Replace the production repository or data-source binding with a deterministic fake in the `androidTest` source set. `@TestInstallIn` preserves the real Hilt graph while preventing network access:

```kotlin
@Module
@TestInstallIn(
    components = [SingletonComponent::class],
    replaces = [ProductionRepositoryModule::class],
)
object TestRepositoryModule {
    @Provides
    @Singleton
    fun provideRepository(): FeedRepository = FakeFeedRepository(
        result = listOf(FeedItem("known-item")),
    )
}
```

The fake should expose predictable success, loading, and failure states so the test can assert user-visible behavior. Do not use a real network or production backend in the test.

For maintainability, split the route from the stateless UI:

```kotlin
@Composable
fun FeedRoute(viewModel: FeedViewModel = hiltViewModel()) {
    val state by viewModel.uiState.collectAsStateWithLifecycle()
    FeedScreen(state = state, onRetry = viewModel::retry)
}

@Composable
fun FeedScreen(state: FeedUiState, onRetry: () -> Unit) {
    // Render only from state and invoke callbacks for user actions.
}
```

Test `FeedScreen` separately with `createComposeRule()` and supplied fake state for fast rendering coverage; keep the Hilt instrumentation test for ViewModel creation, dependency wiring, and state propagation. If the route is inside Navigation, provide the appropriate NavHost/back-stack owner because `hiltViewModel()` is scoped to that owner.


Files in this skill

  • eval-1.baseline.md2.7 KB
  • eval-1.with-skill.md2.4 KB
  • eval-2.baseline.md2.8 KB
  • eval-2.with-skill.md2.5 KB
  • eval-3.baseline.md2.2 KB
  • eval-3.with-skill.md1.8 KB
  • trigger-1.md137 B
  • trigger-10.md139 B
  • trigger-11.md143 B
  • trigger-2.md143 B
  • trigger-3.md139 B
  • trigger-4.md150 B
  • trigger-5.md143 B
  • trigger-6.md146 B
  • trigger-7.md127 B
  • trigger-8.md152 B
  • trigger-9.md142 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.