Agent skill
test-rest-adapter
Generate Kotlin tests using `@WebMvcTest`, `MockMvc`, and `@MockkBean` for a Spring REST controller. Use when the user asks to "write tests for a REST controller", "add MockMvc tests", "generate a @WebMvcTest", "fix an existing @WebMvcTest", or "update the controller test".
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/test-rest-adapter
SKILL.md
Skill: test-rest-adapter
Generate or fix a test for a REST controller using @WebMvcTest with MockMvc and @MockkBean.
Pattern
@WebMvcTest loads only the web layer (no full Spring context, no JPA, no Zeebe).
Use-case dependencies are replaced with @MockkBean from com.ninjasquad.springmockk.
@WebMvcTest(YourController::class)
class YourControllerTest {
@Autowired
private lateinit var mockMvc: MockMvc
@MockkBean
private lateinit var useCase: YourUseCase
private val mapper = ObjectMapper()
@Test
fun `user performs action`() {
// given: valid request body and stubbed use case
every { useCase.method(any()) } returns resultId
// when: the request is performed
val response = mockMvc.perform(
post("/api/path")
.contentType(APPLICATION_JSON).content(mapper.writeValueAsString(input))
).andReturn()
// then: the use case is called with the exact command and the response is correct
verify { useCase.method(expectedCommand) }
confirmVerified(useCase)
}
// Path-variable variant: stub just Runs, no mapper, verify exact domain-id object
}
See references/rest-controller-test-template.kt for the full annotated example.
IMPORTANT
@WebMvcTest(ControllerClass::class)— web slice only; no JPA, no Zeebe@MockkBeanfor every use-case / adapter dependencyprivate val mapper = ObjectMapper()— serialize request bodies via ObjectMapper, not inline strings- Test method names are business-oriented: describe what the user/use-case achieves
(e.g.
`user subscribes to newsletter`), never HTTP mechanics - Structure each test with
// given: <what is set up>,// when: <the action>,// then: <what is verified>sections; omit the section comment when that phase is a single line - Stub use-case with
any()inevery { … }, then verify the exact command object inverify { … } - Use
andReturn()and assert onresponse.response.statusandresponse.response.contentAsStringwith AssertJ confirmVerified(useCase)— detect unexpected calls- Value-type IDs with a
Stringsecondary constructor (e.g.SubscriptionId("uuid-string")) can be used directly - One test per endpoint — happy path only unless error behaviour is explicit in the controller
Instructions
Step 1 – Read and identify
Read the controller file at $ARGUMENTS and extract:
- Controller class name, base
@RequestMappingpath, and each endpoint (@GetMapping,@PostMapping, etc.) - For each endpoint: HTTP method, sub-path,
@RequestBody/@PathVariable/@RequestParamparameters, return type - Injected use-case / adapter dependencies (constructor parameters)
If $ARGUMENTS is empty or the file cannot be read, stop and ask the user to provide the controller path.
Step 2 – Locate the test file
- Derive the test file path by replacing
src/main/kotlinwithsrc/test/kotlinin the controller file path. - Keep the same package structure and class name, but append
Testas a suffix (e.g.NewsletterController→NewsletterControllerTest). - If the test file already exists at that path, open it and switch to fix mode.
- If it does not exist, proceed to generate a new file in Step 4.
Step 3 – Determine required test cases
- Write one happy-path test per endpoint.
- Add an error-case test only when the controller explicitly handles a failure scenario (e.g. returns a specific status code on failure).
- If additional cases apply, list them and ask the user for permission before writing more than one test per endpoint.
Step 4 – Generate or fix the test file
- Use
references/rest-controller-test-template.ktas a starting point - Stub use-case with
any()inevery { }, verify the exact command object inverify { } - Fixed UUID strings for deterministic test data
Step 5 – Write, run, and report
- Write the file to the located path
- Run all tests in the class via an appropriate Gradle command;
- Report the created or updated file path and a brief summary.
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?