# Kotlin Testing

> Kotlin testing - JUnit 5, MockK, Kotest, coroutine testing

- Skill: `pluginagentmarketplace/kotlin-testing` (Agent Skill, multi-file: 6 files)
- Install (CLI): `npx skillmds@latest add pluginagentmarketplace/kotlin-testing`
- Raw SKILL.md: https://api.skillmd.com/api/skills/pluginagentmarketplace/kotlin-testing/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: pluginagentmarketplace (https://skillmd.com/u/pluginagentmarketplace)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/pluginagentmarketplace/kotlin-testing

---


# Kotlin Testing Skill

Comprehensive testing with JUnit 5, MockK, and coroutine testing.

## Topics Covered

### MockK
```kotlin
class UserServiceTest {
    private val repository: UserRepository = mockk()
    private val service = UserService(repository)

    @Test
    fun `createUser saves and returns`() {
        every { repository.save(any()) } returns User(1, "test@test.com")

        val result = service.create(CreateUserRequest("test@test.com"))

        assertThat(result.id).isEqualTo(1)
        verify(exactly = 1) { repository.save(any()) }
    }
}
```

### Coroutine Testing
```kotlin
@Test
fun `loadUser emits states`() = runTest {
    coEvery { repository.getUser(1) } returns Result.success(user)

    viewModel.state.test {
        viewModel.load(1)
        assertThat(awaitItem().isLoading).isTrue()
        advanceUntilIdle()
        assertThat(awaitItem().user).isEqualTo(user)
    }
}
```

### Compose Testing
```kotlin
@Test
fun `button enabled when fields filled`() {
    composeTestRule.setContent { LoginScreen() }
    composeTestRule.onNodeWithTag("email").performTextInput("a@b.com")
    composeTestRule.onNodeWithTag("password").performTextInput("pass")
    composeTestRule.onNodeWithTag("login_button").assertIsEnabled()
}
```

## Troubleshooting

| Issue | Resolution |
|-------|------------|
| "no answer found" | Add every { } returns for method |
| Test hangs | Inject TestDispatcher, use advanceUntilIdle() |

## Usage
```
Skill("kotlin-testing")
```

