cc-codeconductor 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +18 -0
- package/README.md +319 -0
- package/dist/index.js +12741 -0
- package/package.json +51 -0
- package/policy.yml +124 -0
- package/presets/claude/CLAUDE.md +598 -0
- package/presets/claude/commands/cc/feature.md +115 -0
- package/presets/claude/commands/cc/fix.md +121 -0
- package/presets/claude/commands/cc/refactor.md +148 -0
- package/presets/claude/commands/cc/review.md +126 -0
- package/presets/claude/commands/cc/tdd-cycle.md +226 -0
- package/presets/claude/commands/cc/test-plan.md +138 -0
- package/presets/claude/settings.json +37 -0
- package/presets/claude/skills/api-versioning/SKILL.md +389 -0
- package/presets/claude/skills/django-orm/SKILL.md +455 -0
- package/presets/claude/skills/django-testing/SKILL.md +409 -0
- package/presets/claude/skills/jpa-postgres/SKILL.md +618 -0
- package/presets/claude/skills/python/SKILL.md +606 -0
- package/presets/claude/skills/python-django-stack/SKILL.md +492 -0
- package/presets/claude/skills/python-fastapi-stack/SKILL.md +457 -0
- package/presets/claude/skills/spring-boot-feature/SKILL.md +558 -0
- package/presets/claude/skills/spring-boot-kotlin/SKILL.md +400 -0
- package/presets/claude/skills/sqlalchemy/SKILL.md +466 -0
- package/presets/claude/skills/testing-strategy/SKILL.md +479 -0
- package/presets/codex/AGENTS.md +883 -0
- package/presets/codex/README.md +102 -0
- package/presets/codex/skills/api-versioning/SKILL.md +389 -0
- package/presets/codex/skills/django-orm/SKILL.md +455 -0
- package/presets/codex/skills/django-testing/SKILL.md +409 -0
- package/presets/codex/skills/jpa-postgres/SKILL.md +618 -0
- package/presets/codex/skills/python/SKILL.md +606 -0
- package/presets/codex/skills/python-django-stack/SKILL.md +492 -0
- package/presets/codex/skills/python-fastapi-stack/SKILL.md +457 -0
- package/presets/codex/skills/spring-boot-feature/SKILL.md +558 -0
- package/presets/codex/skills/spring-boot-kotlin/SKILL.md +400 -0
- package/presets/codex/skills/sqlalchemy/SKILL.md +466 -0
- package/presets/codex/skills/testing-strategy/SKILL.md +479 -0
- package/presets/opencode/README.md +191 -0
- package/presets/opencode/agents/architect.md +133 -0
- package/presets/opencode/agents/docs.md +113 -0
- package/presets/opencode/agents/implementer.md +116 -0
- package/presets/opencode/agents/orchestrator.md +248 -0
- package/presets/opencode/agents/repo-explorer.md +119 -0
- package/presets/opencode/agents/reviewer.md +130 -0
- package/presets/opencode/agents/task-coach.md +103 -0
- package/presets/opencode/agents/tester.md +269 -0
- package/presets/opencode/commands/cc-feature.md +114 -0
- package/presets/opencode/commands/cc-fix.md +123 -0
- package/presets/opencode/commands/cc-refactor.md +148 -0
- package/presets/opencode/commands/cc-review.md +141 -0
- package/presets/opencode/commands/cc-tdd-cycle.md +225 -0
- package/presets/opencode/commands/cc-test-plan.md +144 -0
- package/presets/opencode/opencode.jsonc +89 -0
- package/presets/opencode/prompts/v0.1.0/architect.md +213 -0
- package/presets/opencode/prompts/v0.1.0/docs.md +181 -0
- package/presets/opencode/prompts/v0.1.0/implementer.md +154 -0
- package/presets/opencode/prompts/v0.1.0/orchestrator.md +169 -0
- package/presets/opencode/prompts/v0.1.0/repo-explorer.md +102 -0
- package/presets/opencode/prompts/v0.1.0/reviewer.md +183 -0
- package/presets/opencode/prompts/v0.1.0/task-coach.md +142 -0
- package/presets/opencode/prompts/v0.1.0/tester.md +160 -0
- package/presets/opencode/prompts/v0.2.0/architect.md +219 -0
- package/presets/opencode/prompts/v0.2.0/docs.md +187 -0
- package/presets/opencode/prompts/v0.2.0/implementer.md +160 -0
- package/presets/opencode/prompts/v0.2.0/orchestrator.md +238 -0
- package/presets/opencode/prompts/v0.2.0/repo-explorer.md +108 -0
- package/presets/opencode/prompts/v0.2.0/reviewer.md +190 -0
- package/presets/opencode/prompts/v0.2.0/task-coach.md +153 -0
- package/presets/opencode/prompts/v0.2.0/tester.md +249 -0
- package/presets/opencode/skills/api-versioning/SKILL.md +388 -0
- package/presets/opencode/skills/astro/SKILL.md +313 -0
- package/presets/opencode/skills/code-review/SKILL.md +202 -0
- package/presets/opencode/skills/django-orm/SKILL.md +455 -0
- package/presets/opencode/skills/django-testing/SKILL.md +408 -0
- package/presets/opencode/skills/django-uv/SKILL.md +400 -0
- package/presets/opencode/skills/jpa-postgres/SKILL.md +617 -0
- package/presets/opencode/skills/nextjs-typescript/SKILL.md +385 -0
- package/presets/opencode/skills/python/SKILL.md +605 -0
- package/presets/opencode/skills/python-django-stack/SKILL.md +491 -0
- package/presets/opencode/skills/python-fastapi-stack/SKILL.md +456 -0
- package/presets/opencode/skills/security/SKILL.md +376 -0
- package/presets/opencode/skills/spring-boot-feature/SKILL.md +557 -0
- package/presets/opencode/skills/spring-boot-kotlin/SKILL.md +399 -0
- package/presets/opencode/skills/spring-boot-testing-strategy/SKILL.md +470 -0
- package/presets/opencode/skills/sqlalchemy/SKILL.md +465 -0
- package/presets/opencode/skills/testing-tdd/SKILL.md +586 -0
- package/src/presets/council/council.yml +59 -0
- package/src/presets/manifests/claude.yml +23 -0
- package/src/presets/manifests/codex.yml +12 -0
- package/src/presets/manifests/opencode.yml +18 -0
- package/src/presets/models/claude.yml +37 -0
- package/src/presets/models/codex.yml +38 -0
- package/src/presets/models/opencode.yml +37 -0
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: testing-strategy
|
|
3
|
+
version: 1.0.0
|
|
4
|
+
name: Testing Strategy
|
|
5
|
+
description:
|
|
6
|
+
Provides expert knowledge of the testing pyramid, test design principles,
|
|
7
|
+
MockK patterns, and integration testing conventions for Spring Boot + Kotlin
|
|
8
|
+
projects.
|
|
9
|
+
|
|
10
|
+
compatibility:
|
|
11
|
+
tools: [claude, codex, opencode]
|
|
12
|
+
stacks:
|
|
13
|
+
languages: [kotlin, java]
|
|
14
|
+
frameworks: [spring-boot, junit5, mockk, testcontainers, assertj]
|
|
15
|
+
databases: [postgresql, h2]
|
|
16
|
+
|
|
17
|
+
risk:
|
|
18
|
+
level: low
|
|
19
|
+
can_execute_shell: false
|
|
20
|
+
can_modify_files: true
|
|
21
|
+
requires_network: false
|
|
22
|
+
|
|
23
|
+
inputs:
|
|
24
|
+
- source_files
|
|
25
|
+
- test_files
|
|
26
|
+
- build.gradle.kts
|
|
27
|
+
|
|
28
|
+
outputs:
|
|
29
|
+
- unit test classes
|
|
30
|
+
- integration test classes
|
|
31
|
+
- test data factories
|
|
32
|
+
- contract test scaffolding
|
|
33
|
+
|
|
34
|
+
quality:
|
|
35
|
+
reviewed_by: codeconductor-core
|
|
36
|
+
version: 0.1.0
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
# Testing Strategy
|
|
40
|
+
|
|
41
|
+
## Testing Pyramid
|
|
42
|
+
|
|
43
|
+
```text
|
|
44
|
+
/\
|
|
45
|
+
/ \
|
|
46
|
+
/ E2E\ 10% — full API, happy path + main error cases
|
|
47
|
+
/------\
|
|
48
|
+
/ Integ \ 20% — components with real dependencies (DB, HTTP)
|
|
49
|
+
/----------\
|
|
50
|
+
/ Unit \ 70% — isolated, mocked dependencies, fast
|
|
51
|
+
/______________\
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Unit tests are the foundation. They are fast, deterministic, and cheap to run.
|
|
55
|
+
Integration tests validate that components work together. E2E tests validate
|
|
56
|
+
that the system works end to end — keep them minimal.
|
|
57
|
+
|
|
58
|
+
If you find yourself writing more integration tests than unit tests, the code
|
|
59
|
+
under test has too many responsibilities bundled together.
|
|
60
|
+
|
|
61
|
+
## Test Naming Convention
|
|
62
|
+
|
|
63
|
+
Format: `should [expected behavior] when [condition]`
|
|
64
|
+
|
|
65
|
+
```kotlin
|
|
66
|
+
@Test
|
|
67
|
+
fun `should return user when found by id`() { ... }
|
|
68
|
+
|
|
69
|
+
@Test
|
|
70
|
+
fun `should return 404 when user does not exist`() { ... }
|
|
71
|
+
|
|
72
|
+
@Test
|
|
73
|
+
fun `should throw ConflictException when email already exists`() { ... }
|
|
74
|
+
|
|
75
|
+
@Test
|
|
76
|
+
fun `should not return deleted users in list`() { ... }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Group related tests with `@Nested`:
|
|
80
|
+
|
|
81
|
+
```kotlin
|
|
82
|
+
@ExtendWith(MockKExtension::class)
|
|
83
|
+
class UserServiceTest {
|
|
84
|
+
|
|
85
|
+
@Nested
|
|
86
|
+
inner class GetById {
|
|
87
|
+
@Test
|
|
88
|
+
fun `should return user when found`() { ... }
|
|
89
|
+
|
|
90
|
+
@Test
|
|
91
|
+
fun `should return NotFound when user does not exist`() { ... }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@Nested
|
|
95
|
+
inner class Create {
|
|
96
|
+
@Test
|
|
97
|
+
fun `should create and return user when email is unique`() { ... }
|
|
98
|
+
|
|
99
|
+
@Test
|
|
100
|
+
fun `should throw ConflictException when email already exists`() { ... }
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Unit Test Structure (AAA)
|
|
106
|
+
|
|
107
|
+
Every test follows Arrange → Act → Assert. Use blank lines to separate each
|
|
108
|
+
phase.
|
|
109
|
+
|
|
110
|
+
```kotlin
|
|
111
|
+
@Test
|
|
112
|
+
fun `should return user when found by id`() {
|
|
113
|
+
// Arrange
|
|
114
|
+
val userId = UUID.randomUUID()
|
|
115
|
+
val user = User(id = userId, email = "user@example.com", name = "Test User")
|
|
116
|
+
every { userRepository.findById(userId) } returns Optional.of(user)
|
|
117
|
+
|
|
118
|
+
// Act
|
|
119
|
+
val result = userService.getById(userId)
|
|
120
|
+
|
|
121
|
+
// Assert
|
|
122
|
+
assertThat(result).isInstanceOf(UserResult.Found::class.java)
|
|
123
|
+
val found = result as UserResult.Found
|
|
124
|
+
assertThat(found.user.id).isEqualTo(userId)
|
|
125
|
+
verify(exactly = 1) { userRepository.findById(userId) }
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
No inline comments between phases once the structure is clear. The blank lines
|
|
130
|
+
are enough.
|
|
131
|
+
|
|
132
|
+
## What NOT to Test
|
|
133
|
+
|
|
134
|
+
**Framework wiring.** Spring handles dependency injection. Do not write tests
|
|
135
|
+
that verify `@Autowired` works.
|
|
136
|
+
|
|
137
|
+
**JPA mapping.** Do not test that `@Column(name = "email")` maps to the right
|
|
138
|
+
column. That is Hibernate's job.
|
|
139
|
+
|
|
140
|
+
**Trivial getters and setters.** A data class property has no logic. There is
|
|
141
|
+
nothing to test.
|
|
142
|
+
|
|
143
|
+
**Private methods directly.** Private methods are implementation details. Test
|
|
144
|
+
them through the public behavior that uses them. If a private method is complex
|
|
145
|
+
enough to need its own test, it should be extracted into a separate class.
|
|
146
|
+
|
|
147
|
+
**Implementation, not behavior.** Tests that verify HOW something is done (mock
|
|
148
|
+
call order, internal state) are brittle. Test WHAT the output or side effect is.
|
|
149
|
+
|
|
150
|
+
```kotlin
|
|
151
|
+
// bad — tests implementation detail
|
|
152
|
+
verify { userRepository.findById(any()) }
|
|
153
|
+
verify { cacheService.put(any(), any()) }
|
|
154
|
+
verify(ordering = Ordering.ORDERED) { // this is too coupled to internals
|
|
155
|
+
userRepository.findById(userId)
|
|
156
|
+
cacheService.put(userId, user)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// good — tests observable behavior
|
|
160
|
+
assertThat(result).isEqualTo(expectedUser)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## MockK Patterns
|
|
164
|
+
|
|
165
|
+
### Basic Setup
|
|
166
|
+
|
|
167
|
+
```kotlin
|
|
168
|
+
@ExtendWith(MockKExtension::class)
|
|
169
|
+
class UserServiceTest {
|
|
170
|
+
|
|
171
|
+
@MockK
|
|
172
|
+
private lateinit var userRepository: UserRepository
|
|
173
|
+
|
|
174
|
+
@MockK
|
|
175
|
+
private lateinit var emailService: EmailService
|
|
176
|
+
|
|
177
|
+
private lateinit var userService: UserService
|
|
178
|
+
|
|
179
|
+
@BeforeEach
|
|
180
|
+
fun setUp() {
|
|
181
|
+
userService = UserService(userRepository, emailService)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Stubbing
|
|
187
|
+
|
|
188
|
+
```kotlin
|
|
189
|
+
// Return value
|
|
190
|
+
every { userRepository.findById(userId) } returns Optional.of(user)
|
|
191
|
+
|
|
192
|
+
// Return null (for nullable return types)
|
|
193
|
+
every { userRepository.findByEmail(any()) } returns null
|
|
194
|
+
|
|
195
|
+
// Throw exception
|
|
196
|
+
every { userRepository.save(any()) } throws DataIntegrityViolationException("Duplicate")
|
|
197
|
+
|
|
198
|
+
// Return different values on successive calls
|
|
199
|
+
every { userRepository.findById(any()) } returnsMany listOf(Optional.of(user), Optional.empty())
|
|
200
|
+
|
|
201
|
+
// Answer with computation
|
|
202
|
+
every { userRepository.save(any()) } answers { firstArg() }
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Verification
|
|
206
|
+
|
|
207
|
+
```kotlin
|
|
208
|
+
// Verify called exactly once with specific argument
|
|
209
|
+
verify(exactly = 1) { userRepository.findById(userId) }
|
|
210
|
+
|
|
211
|
+
// Verify called with any argument
|
|
212
|
+
verify { emailService.sendWelcome(any()) }
|
|
213
|
+
|
|
214
|
+
// Verify never called
|
|
215
|
+
verify(exactly = 0) { emailService.sendWelcome(any()) }
|
|
216
|
+
|
|
217
|
+
// Verify called with specific argument
|
|
218
|
+
verify { emailService.sendWelcome(match { it.email == "user@example.com" }) }
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Relaxed Mocks
|
|
222
|
+
|
|
223
|
+
```kotlin
|
|
224
|
+
// relaxed = true — returns default values (null, 0, false, empty list)
|
|
225
|
+
// Use only when you do not care about the return value of any method
|
|
226
|
+
val logger = mockk<Logger>(relaxed = true)
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Do not use `relaxed = true` on mocks where you need to verify behavior — a
|
|
230
|
+
relaxed mock does not fail on unexpected calls, which can hide bugs.
|
|
231
|
+
|
|
232
|
+
### Coroutines
|
|
233
|
+
|
|
234
|
+
```kotlin
|
|
235
|
+
// Stubbing suspend functions
|
|
236
|
+
coEvery { userRepository.findById(userId) } returns user
|
|
237
|
+
|
|
238
|
+
// Verifying suspend functions
|
|
239
|
+
coVerify(exactly = 1) { userRepository.findById(userId) }
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Argument Matchers
|
|
243
|
+
|
|
244
|
+
```kotlin
|
|
245
|
+
// Any value
|
|
246
|
+
every { service.process(any()) } returns result
|
|
247
|
+
|
|
248
|
+
// Specific type
|
|
249
|
+
every { service.process(any<UserRequest>()) } returns result
|
|
250
|
+
|
|
251
|
+
// Custom predicate
|
|
252
|
+
every { service.notify(match { it.email.endsWith("@example.com") }) } just Runs
|
|
253
|
+
|
|
254
|
+
// Capture for assertion
|
|
255
|
+
val slot = slot<User>()
|
|
256
|
+
every { userRepository.save(capture(slot)) } returns savedUser
|
|
257
|
+
// ... trigger the action ...
|
|
258
|
+
assertThat(slot.captured.email).isEqualTo("expected@example.com")
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## Integration Test Patterns
|
|
262
|
+
|
|
263
|
+
### Controller Tests with MockMvc
|
|
264
|
+
|
|
265
|
+
```kotlin
|
|
266
|
+
@SpringBootTest
|
|
267
|
+
@AutoConfigureMockMvc
|
|
268
|
+
class UserControllerTest {
|
|
269
|
+
|
|
270
|
+
@Autowired
|
|
271
|
+
private lateinit var mockMvc: MockMvc
|
|
272
|
+
|
|
273
|
+
@Autowired
|
|
274
|
+
private lateinit var objectMapper: ObjectMapper
|
|
275
|
+
|
|
276
|
+
@Test
|
|
277
|
+
fun `should return 200 with user when found`() {
|
|
278
|
+
mockMvc.perform(
|
|
279
|
+
get("/api/v1/users/${existingUser.id}")
|
|
280
|
+
.contentType(MediaType.APPLICATION_JSON)
|
|
281
|
+
)
|
|
282
|
+
.andExpect(status().isOk)
|
|
283
|
+
.andExpect(jsonPath("$.email").value(existingUser.email))
|
|
284
|
+
.andExpect(jsonPath("$.id").value(existingUser.id.toString()))
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
@Test
|
|
288
|
+
fun `should return 400 when request body is invalid`() {
|
|
289
|
+
val invalidRequest = mapOf("email" to "not-an-email")
|
|
290
|
+
|
|
291
|
+
mockMvc.perform(
|
|
292
|
+
post("/api/v1/users")
|
|
293
|
+
.contentType(MediaType.APPLICATION_JSON)
|
|
294
|
+
.content(objectMapper.writeValueAsString(invalidRequest))
|
|
295
|
+
)
|
|
296
|
+
.andExpect(status().isBadRequest)
|
|
297
|
+
.andExpect(jsonPath("$.code").value("VALIDATION_ERROR"))
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
@Test
|
|
301
|
+
fun `should return 404 when user is not found`() {
|
|
302
|
+
mockMvc.perform(get("/api/v1/users/${UUID.randomUUID()}"))
|
|
303
|
+
.andExpect(status().isNotFound)
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
`@AutoConfigureMockMvc` configures MockMvc without starting a real HTTP server.
|
|
309
|
+
The test is faster than `RANDOM_PORT` but still tests the full Spring MVC stack.
|
|
310
|
+
|
|
311
|
+
Use `RANDOM_PORT` only when you need real HTTP (e.g., testing HTTP client
|
|
312
|
+
behavior, WebSockets, or SSE).
|
|
313
|
+
|
|
314
|
+
### Database Tests with Testcontainers
|
|
315
|
+
|
|
316
|
+
```kotlin
|
|
317
|
+
@SpringBootTest
|
|
318
|
+
@Testcontainers
|
|
319
|
+
class UserRepositoryIntegrationTest {
|
|
320
|
+
|
|
321
|
+
companion object {
|
|
322
|
+
@Container
|
|
323
|
+
@JvmStatic
|
|
324
|
+
val postgres = PostgreSQLContainer<Nothing>("postgres:16").apply {
|
|
325
|
+
withDatabaseName("testdb")
|
|
326
|
+
withUsername("test")
|
|
327
|
+
withPassword("test")
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
@DynamicPropertySource
|
|
331
|
+
@JvmStatic
|
|
332
|
+
fun overrideProperties(registry: DynamicPropertyRegistry) {
|
|
333
|
+
registry.add("spring.datasource.url", postgres::getJdbcUrl)
|
|
334
|
+
registry.add("spring.datasource.username", postgres::getUsername)
|
|
335
|
+
registry.add("spring.datasource.password", postgres::getPassword)
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
@Autowired
|
|
340
|
+
private lateinit var userRepository: UserRepository
|
|
341
|
+
|
|
342
|
+
@Test
|
|
343
|
+
fun `should enforce unique email at database level`() {
|
|
344
|
+
userRepository.save(User(email = "unique@example.com", name = "First"))
|
|
345
|
+
assertThrows<DataIntegrityViolationException> {
|
|
346
|
+
userRepository.save(User(email = "unique@example.com", name = "Second"))
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
Share the container across test classes using a base class with `@Container` on
|
|
353
|
+
a static field. Testcontainers reuses the same container if the configuration is
|
|
354
|
+
identical.
|
|
355
|
+
|
|
356
|
+
### Seed Data with @Sql
|
|
357
|
+
|
|
358
|
+
```kotlin
|
|
359
|
+
@SpringBootTest
|
|
360
|
+
@AutoConfigureMockMvc
|
|
361
|
+
@Sql("/test-data/users.sql") // runs before each test in this class
|
|
362
|
+
class UserControllerTest { ... }
|
|
363
|
+
|
|
364
|
+
// Or per-test
|
|
365
|
+
@Test
|
|
366
|
+
@Sql("/test-data/users-with-orders.sql")
|
|
367
|
+
fun `should return orders when user has orders`() { ... }
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
SQL files go in `src/test/resources/test-data/`. Use descriptive names that
|
|
371
|
+
explain what state they create, not what feature they serve.
|
|
372
|
+
|
|
373
|
+
## Test Data Factories
|
|
374
|
+
|
|
375
|
+
Never hardcode UUIDs or share mutable state between tests.
|
|
376
|
+
|
|
377
|
+
```kotlin
|
|
378
|
+
object UserTestFactory {
|
|
379
|
+
|
|
380
|
+
fun create(
|
|
381
|
+
id: UUID = UUID.randomUUID(),
|
|
382
|
+
email: String = "user-${UUID.randomUUID()}@example.com",
|
|
383
|
+
name: String = "Test User",
|
|
384
|
+
role: UserRole = UserRole.USER
|
|
385
|
+
): User = User(
|
|
386
|
+
id = id,
|
|
387
|
+
email = email,
|
|
388
|
+
name = name,
|
|
389
|
+
role = role
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
fun createRequest(
|
|
393
|
+
email: String = "user-${UUID.randomUUID()}@example.com",
|
|
394
|
+
name: String = "Test User"
|
|
395
|
+
): CreateUserRequest = CreateUserRequest(
|
|
396
|
+
email = email,
|
|
397
|
+
name = name
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
fun createAdmin(): User = create(role = UserRole.ADMIN)
|
|
401
|
+
|
|
402
|
+
fun createList(count: Int): List<User> = (1..count).map { create() }
|
|
403
|
+
}
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
Usage in tests:
|
|
407
|
+
|
|
408
|
+
```kotlin
|
|
409
|
+
val user = UserTestFactory.create(email = "specific@example.com")
|
|
410
|
+
val admin = UserTestFactory.createAdmin()
|
|
411
|
+
val users = UserTestFactory.createList(5)
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
Default values use `UUID.randomUUID()` to ensure tests do not share state
|
|
415
|
+
through hardcoded IDs or emails.
|
|
416
|
+
|
|
417
|
+
## When Tests Are Required
|
|
418
|
+
|
|
419
|
+
**New behavior.** Every new feature requires at minimum one unit test and one
|
|
420
|
+
integration test covering the happy path. Edge cases and error paths require
|
|
421
|
+
additional unit tests.
|
|
422
|
+
|
|
423
|
+
**Bug fix.** Write a regression test that reproduces the bug before fixing it.
|
|
424
|
+
The test must fail before the fix and pass after. This prevents the bug from
|
|
425
|
+
reappearing silently.
|
|
426
|
+
|
|
427
|
+
```kotlin
|
|
428
|
+
// Example: bug where findByEmail returned deleted users
|
|
429
|
+
@Test
|
|
430
|
+
fun `should not return deleted user — regression for bug #42`() {
|
|
431
|
+
val user = userRepository.save(UserTestFactory.create(email = "gone@example.com"))
|
|
432
|
+
userRepository.delete(user)
|
|
433
|
+
val result = userRepository.findByEmail("gone@example.com")
|
|
434
|
+
assertThat(result).isNull()
|
|
435
|
+
}
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
**Refactor.** Do not write new tests. The existing tests must pass unchanged. If
|
|
439
|
+
they do not, the refactor changed behavior — that is a bug.
|
|
440
|
+
|
|
441
|
+
**High-risk paths.** Security, payment processing, data migrations, and
|
|
442
|
+
permission checks require contract tests in addition to unit and integration
|
|
443
|
+
tests. These paths have severe consequences if broken.
|
|
444
|
+
|
|
445
|
+
## CI Integration
|
|
446
|
+
|
|
447
|
+
Tests must run in CI on every pull request. The pipeline must fail if any test
|
|
448
|
+
fails.
|
|
449
|
+
|
|
450
|
+
Recommended test execution order:
|
|
451
|
+
|
|
452
|
+
1. Unit tests (fastest — fail fast)
|
|
453
|
+
2. Integration tests with H2 (`@DataJpaTest`)
|
|
454
|
+
3. Integration tests with Testcontainers (slowest — run last)
|
|
455
|
+
|
|
456
|
+
Separate test tasks in Gradle:
|
|
457
|
+
|
|
458
|
+
```kotlin
|
|
459
|
+
// build.gradle.kts
|
|
460
|
+
tasks.register<Test>("integrationTest") {
|
|
461
|
+
description = "Runs integration tests"
|
|
462
|
+
group = "verification"
|
|
463
|
+
useJUnitPlatform {
|
|
464
|
+
includeTags("integration")
|
|
465
|
+
}
|
|
466
|
+
shouldRunAfter(tasks.test)
|
|
467
|
+
}
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
Tag integration tests:
|
|
471
|
+
|
|
472
|
+
```kotlin
|
|
473
|
+
@Tag("integration")
|
|
474
|
+
@SpringBootTest
|
|
475
|
+
class UserRepositoryIntegrationTest { ... }
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
Do not mix unit and integration test tags in the same class. A test class is
|
|
479
|
+
either one or the other.
|