Kotlin testing - JUnit 5, MockK, Kotest, coroutine testing
Kotlin Testing Skill
Comprehensive testing with JUnit 5, MockK, and coroutine testing.
Topics Covered
MockK
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
@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
@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")
You Might Also Like
Related Skills

fix
Use when you have lint errors, formatting issues, or before committing code to ensure it passes CI.
facebook
frontend-testing
Generate Vitest + React Testing Library tests for Dify frontend components, hooks, and utilities. Triggers on testing, spec files, coverage, Vitest, RTL, unit tests, integration tests, or write/review test requests.
langgenius
frontend-code-review
Trigger when the user requests a review of frontend files (e.g., `.tsx`, `.ts`, `.js`). Support both pending-change reviews and focused file reviews while applying the checklist rules.
langgenius
code-reviewer
Use this skill to review code. It supports both local changes (staged or working tree) and remote Pull Requests (by ID or URL). It focuses on correctness, maintainability, and adherence to project standards.
google-gemini
session-logs
Search and analyze your own session logs (older/parent conversations) using jq.
moltbot

