Generate Robust Android Unit Tests
An Android unit-testing skill covering JUnit 5, Mockito, and Kotlin coroutines for ViewModels, Repositories, and Use Cases in clean architecture.
1.0.0Add to Favorites
Why it matters
Automate the creation of comprehensive Android unit tests. This asset specializes in generating tests for ViewModels, Repositories, Use Cases, and business logic, ensuring code quality and maintainability.
Outcomes
What it gets done
Write unit tests for Android ViewModels using JUnit 5 and Mockito.
Generate tests for Repositories and Use Cases following clean architecture principles.
Implement tests for business logic with a focus on SOLID principles.
Ensure tests adhere to AAA pattern, determinism, and fast execution.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/vb-android-unit-test | bash Overview
Android Unit Test Expert Agent
A skill providing worked Android unit test examples in Kotlin using JUnit 5, Mockito, and coroutines test utilities for ViewModels, Repositories, and Use Cases. It covers the AAA testing pattern, a custom MainDispatcherRule for coroutine tests, StateFlow/SharedFlow testing, and parameterized test patterns. Use it when writing unit tests for Android ViewModels, Repositories, or Use Cases in a Kotlin coroutines codebase that needs fast, deterministic tests with all external dependencies mocked - not for instrumented UI or end-to-end tests.
What it does
This skill provides expert Android unit testing patterns using JUnit 5, Mockito, Kotlin, and modern Android testing practices, focused on testing ViewModels, Repositories, Use Cases, and business logic under SOLID principles and clean architecture. Its core testing principles are the AAA pattern (Arrange, Act, Assert), single responsibility per test, deterministic results regardless of execution order, fast dependency-free execution, and readable test names using backtick-quoted descriptive sentences. It provides worked examples for each architectural layer: Repository tests that mock an API service and local data source and verify caching behavior, ViewModel tests using a MainDispatcherRule and runTest to assert UI state transitions on success and failure, Repository-pattern tests covering network-failure-falls-back-to-cache scenarios, and Use Case tests that combine two mocked repositories into one enhanced result. It also covers a TestDataFactory pattern for reusable test data, custom argThat matchers, Result extension functions (shouldBeSuccess/shouldBeFailure), StateFlow/SharedFlow testing with UnconfinedTestDispatcher, and parameterized tests via @ParameterizedTest and @ValueSource.
When to use - and when NOT to
Use this skill when writing or reviewing unit tests for Android app logic - ViewModels, Repositories, Use Cases - that follow clean architecture and need to stay fast and deterministic by mocking external dependencies. It is built around JUnit 5, Mockito, Kotlin coroutines test utilities, and the Truth assertion library specifically, so it fits projects already on or moving to that stack. Its parameterized-test example validates an EmailValidator against a set of bad inputs (empty string, whitespace-only, a malformed address) in one @ValueSource-driven test rather than three separate copy-pasted test methods. It is not for instrumented UI or end-to-end tests - every example here mocks the API service, local data source, and cache manager rather than hitting a real network or database.
Inputs and outputs
// build.gradle.kts (app module)
testImplementation("junit:junit:4.13.2")
testImplementation("org.mockito:mockito-core:4.6.1")
testImplementation("org.mockito.kotlin:mockito-kotlin:4.0.0")
testImplementation("androidx.arch.core:core-testing:2.2.0")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
testImplementation("com.google.truth:truth:1.1.3")
testImplementation("app.cash.turbine:turbine:0.12.1")
Given a class to test, the skill outputs complete Kotlin test classes with @Mock-annotated dependencies, @BeforeEach setup using MockitoAnnotations.openMocks, and test methods following the AAA structure with whenever/verify Mockito syntax and Truth's assertThat assertions. It also supplies a custom MainDispatcherRule (a TestWatcher that sets and resets the main coroutine dispatcher) for coroutine-based ViewModel tests, plus reusable test-data-factory and Result-extension helper code.
Who it's for
Android engineers writing unit tests for ViewModels, Repositories, and Use Cases in a Kotlin coroutines codebase who want concrete, copy-adaptable examples rather than testing theory - every pattern here is a full worked test class, not a snippet. It suits teams that want mocks reset and state cleared between tests via @BeforeEach as standard discipline, not something each engineer reinvents per test file, alongside consistent use of JUnit 5, Mockito, Truth, and Turbine for coroutine/Flow testing.
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.