prasad-vennam/awesome-android-ai-agent-skills

android-unit-testing

Use when writing or fixing unit tests, mocking objects, or testing reactive flows in Android.

소스 보기

사용 방법

  1. 설치 명령을 복사하고 자신의 에이전트 환경에 설치하기 전에 소스 패키지를 확인하세요.
  2. 필요한 도구, 인증 정보, 지원 환경은 원본 문서에서 확인하세요.
  3. 입력과 원하는 산출물을 설명하고 출처와 결과를 대조하세요.
npx skills add https://github.com/prasad-vennam/awesome-android-ai-agent-skills --skill android-unit-testing

시작 전 확인

  • 설치에는 모델 구독, API 한도, 파일 접근 권한이 포함되지 않습니다.
  • 별점 수와 설치 수는 활동 지표이며 독립적인 품질·보안 검증이 아닙니다.
원본 Skill 문서

원본 저장소의 제목, 예시, 코드, 표, 링크, 이미지를 유지해 표시합니다.

Android Unit Testing Excellence 🧪🛡️

The ultimate guide to writing robust, reliable, and maintainable unit tests for Android applications using modern tools like MockK, Turbine, and Coroutine Test.

⚡ When to Use

  • Business Logic Verification: Testing Use Cases or ViewModels.
  • Data Layer Testing: Testing Repositories or Data Sources (with Fakes).
  • Reactive Stream Testing: Verifying emitted values from Flow and StateFlow.
  • Logic Refactoring: Ensuring no regressions during code changes.
  • TDD (Test-Driven Development): Writing tests before implementation.

🏗️ Core Tools

1. JUnit 5 (Modern Standard)

  • Use @Test, @BeforeEach, and @DisplayName for clear tests.
  • Rule: Prefer JUnit 5 for new projects, but JUnit 4 is common in legacy.

2. MockK (Power Mocking)

  • Rule: Use MockK for Kotlin-first mocking.
  • Shorthand: every { ... } returns ... and coEvery { ... } returns ... for suspend functions.
kotlin
  val repository = mockk<UserRepository>()
  coEvery { repository.getUser("123") } returns User("John")

3. Turbine (Flow Testing)

  • Rule: Never use collect or first() in tests manually. Use Turbine.
kotlin
  flow.test {
      assertEquals(1, awaitItem())
      assertEquals(2, awaitItem())
      awaitComplete()
  }

🚀 Coroutine Testing (Essentials)

Use runTest

  • runTest skips delay() and ensures tests run fast and predictably.

Inject Dispatchers

  • Rule: NEVER hardcode Dispatchers.IO or Main. Inject them.
  • In Tests: Use StandardTestDispatcher or UnconfinedTestDispatcher.

🧪 Testing Patterns

1. ViewModels

  • Standard: Test the Initial State → Trigger Action → Verify Final State and/or Side Effects.
  • Note: Ensure Dispatchers.setMain(testDispatcher) is called before and Dispatchers.resetMain() after tests.

2. Repositories

  • Standard: Test that data is correctly mapped and error handling is resilient.
  • Fakes: Use FakeLocalSource or FakeRemoteSource for complex integration tests.

3. Domain Use Cases

  • Standard: Test that the use case calls the repository and applies business rules correctly.

📦 Assertion Libraries

  • Truth (from Google): Fluent, readable assertions.
  • AssertJ: Extremely powerful and comprehensive.
  • Kotest Assertions: Native Kotlin syntax (e.g., user.name shouldBe "John").

🚀 Testing Checklist

  • [ ] Method Names: Use descriptive names like test_whenQueryChanged_updatesState.
  • [ ] One Assertion per Test: (Ideally) One logical assertion per test for better failure isolation.
  • [ ] Arrange-Act-Assert (AAA): Follow this structure for readability.
  • [ ] Verify Mocks: Check if expected methods were called using verify { ... }.

🔗 Related Resources

관련 작업

같은 저장소의 Skills

더 많은 Skills

모든 Skills