Create Kotlin Tests
Workflow
- •Identify the subject under test and pick the correct base class:
- •
UnitTestfor pure unit tests. - •
JpaTestfor repositories and query behavior. - •
ApiTestfor REST contracts, authorization, and controller behavior; mock dependencies. - •
IntegrationTestfor end-to-end or multi-layer flows.
- •
- •For every tested function, create an
inner classgrouping its test cases. - •Cover test cases to target 100% code coverage for the function:
- •Best-case (happy path)
- •Error scenarios
- •Edge cases
- •Use JUnit for assertions and MockK for mocking.
Kotlin + Spring Boot Testing Rules
- •All tests must extend one of:
UnitTest,JpaTest,ApiTest, orIntegrationTest. - •Prefer
@Mockkand@InjectMockkinUnitTest(MockK already set up there). - •
JpaTestis primarily for repository tests and query validation. - •
ApiTestis for REST endpoint contracts, auth behavior, and should mock its dependencies. - •For mocking Spring beans, create a configuration subclass and include it in the test; do not use MockKBean/MockitoBean.
- •Do not mock data classes.
- •Exception assertions: assert only the exception type, not the message.
- •Do not use comments like
// Arrange,// Act,// Assert. - •If a test function name is not descriptive enough, add a brief comment on the function describing the test case.
Output Expectations
- •Group tests by function using
inner class. - •Use clear, consistent naming for test methods; add a short comment only when the name is not self-descriptive.
- •Ensure coverage includes best-case, error, and edge cases for each function.