Skip to content
Back to skills

Kotlin Spring Testing

ASecurity

Test patterns for a Kotlin Spring Boot backend: choosing the layer, slice tests, body tests validated against the OpenAPI contract, Testcontainers, MockK, tagging tests with acceptance-criteria IDs, and keeping runs fast. Load when writing or fixing backend tests.

  • 13 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 6, 2026
ai-agentskotlinexpressspringdockertestingapibackend

Works with

  • api

Security analysis

A100/100

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

Scanned November 6, 2026

npx -y skills add MiladNalbandi/keel-v2 --skill kotlin-spring-testing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Kotlin Spring Testing?

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

Security grade badge for Kotlin Spring Testing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/miladnalbandi-kotlin-spring-testing-keel-v2/badge)](https://www.skillsdirectory.com/skills/miladnalbandi-kotlin-spring-testing-keel-v2)

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
---
name: kotlin-spring-testing
description: "Test patterns for a Kotlin Spring Boot backend: choosing the layer, slice tests, body tests validated against the OpenAPI contract, Testcontainers, MockK, tagging tests with acceptance-criteria IDs, and keeping runs fast. Load when writing or fixing backend tests."
user-invocable: false
---

# Kotlin + Spring Boot test patterns

## Pick the lowest layer that can express the AC

| Layer | Use | Annotation |
|---|---|---|
| Unit | Pure rules, no Spring | none, JUnit 5 + MockK + AssertJ |
| Web slice | Status codes, validation, auth rules | `@WebMvcTest` + MockMvc (+ `@MockkBean`) |
| Body / contract | Request and response bodies conform to the contract and carry the right values | MockMvc + `swagger-request-validator-mockmvc` |
| Data slice | Queries, constraints, migrations | `@DataJpaTest` + Testcontainers |
| Integration | Cross-layer flows, transactions | `@SpringBootTest` + `@ServiceConnection` |

## Tag every test with its AC

```kotlin
@Tag("AC-003")
@Test
fun `AC-003 rejects a bookmark without a url`() { }
```

keel runs one criterion's test by that tag, and traces the test back to the criterion by the ID in its name.

## Body test shape

```kotlin
@WebMvcTest(BookmarkController::class)
@Import(OpenApiValidationConfig::class)   // adds the swagger-request-validator filter
class BookmarkBodyTest(@Autowired val mvc: MockMvc) {

  @Tag("AC-002")
  @Test
  fun `AC-002 created bookmark body matches the contract`() {
    mvc.post("/bookmarks") { contentType = APPLICATION_JSON; content = """{"url":"https://x.dev"}""" }
      .andExpect { status { isCreated() } }
      .andExpect { jsonPath("$.id") { exists() } }
      .andExpect { jsonPath("$.url") { value("https://x.dev") } }
      // the validator filter fails the test when the body breaks openapi.yaml
  }
}
```

## Keep runs fast

- One shared test configuration with singleton containers; every different context configuration starts Spring again.
- Testcontainers reuse locally: `withReuse(true)` plus `testcontainers.reuse.enable=true`.
- Avoid `@DirtiesContext` and per-class mock variations.
- Prefer slice tests over `@SpringBootTest` unless the AC needs the whole app.

## Rules keel enforces

- In RED only test files may change; in GREEN only production code.
- Never add `@Disabled`, `@Ignore` or `assumeTrue(false)`: a parked test is a weakened test, and review blocks it.
- A failure from a compile error, a missing bean or Docker is not a red test; fix the setup first.

Files in this skill

  • SKILL.md2.5 KB
  • examples/BookmarkBodyTest.kt2.5 KB
  • examples/BookmarkControllerTest.kt2 KB

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 ai-agents →

Comments

Loading comments…

Newsletter

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