2.1 KiB
2.1 KiB
name, description
| name | description |
|---|---|
| writing-tests | werkator testing conventions — Kotest FunSpec spec structure, MockK matchers, and the two patterns for mocking beans in Spring slice tests (springmockk @MockkBean or @TestConfiguration). Use when writing, extending, or refactoring tests. |
Writing Tests for werkator
Tests use Kotest FunSpec style. SpringExtension is registered globally in io.kotest.provided.ProjectConfig — do not add it per-spec.
class MyTest : FunSpec() {
init {
test("description") { ... }
beforeEach { ... }
}
}
Use shouldBe, shouldNotBe, shouldThrow etc. from io.kotest.matchers.
Tests mirror the production package structure under src/test/kotlin.
Run a single test class instead of the full suite while iterating:
./gradlew test --tests "de.hoennig.werkator.ApplicationContextTest"
Mocking in Spring Slice Tests
Use @MockkBean from springmockk to inject MockK mocks into the Spring context:
@WebMvcTest(SomeController::class)
class SomeControllerTest : FunSpec() {
@MockkBean
lateinit var someService: SomeService
init {
beforeEach { clearMocks(someService) }
// full MockK syntax: every { } / verify { }
}
}
Alternatively, register mocks via @TestConfiguration without the springmockk dependency:
@WebMvcTest(SomeController::class)
@Import(SomeControllerTest.Mocks::class)
class SomeControllerTest : FunSpec() {
@TestConfiguration
class Mocks {
@Bean fun someService(): SomeService = mockk()
}
@Autowired lateinit var someService: SomeService
init {
beforeEach { clearMocks(someService) }
}
}
Pure unit tests (no Spring context) use MockK directly without any Spring wiring.
Test Infrastructure by Layer
- Git-facing code: integration tests against local fixture repositories (bare origin + clones), see
GitServiceTest— no network access, hermetic git environment variables. - HTTP clients (Gitea): WireMock.
- Docker-dependent code: Testcontainers.
- The
Watcherand other schedulers never start their loops in tests; callpoll()/lifecycle methods directly.