backend-testing-guideline
Skill: backend-testing-guideline
Purpose
Ensure generated backend tests are:
- module-aware
- API-contract-safe
- aligned with Spring Boot architecture
- practical for business workflows
- consistent with OpenAPI, DB schema, and shared response rules
Scope
Use this skill when generating or reviewing backend tests for:
- controllers
- services
- mappers
- auth/security behavior
- validation
- pagination
- soft delete
- module business rules
Backend Test Location Rule
Backend tests should live under:
backend/src/test/java/com/yourapp/modules/<module_name>/
Possible structure:
controller/service/mapper/integration/
Shared backend test helpers may live under:
backend/src/test/java/com/yourapp/testsupport/
Only create shared test helpers when there is a real reuse need.
Testing Stack Assumption
Preferred backend testing tools:
- JUnit 5
- Spring Boot Test
- MockMvc
- Mockito
- AssertJ or standard assertion style
- transactional/integration DB testing where appropriate
Do not introduce an unrelated testing stack without a strong reason.
Coverage Priorities
Prioritize tests for:
- critical business rules
- API contract correctness
- validation behavior
- security/auth boundaries
- pagination and wrapper correctness
- soft delete behavior
- error handling and edge cases
Avoid spending most effort on trivial getters/setters or noise-level tests.
Controller Test Rule
Controller/API tests should verify:
- correct HTTP method and path behavior
- request validation behavior
- response HTTP status
- response wrapper shape
- pagination wrapper shape
- auth/security access rules when applicable
- error path expectations for invalid or missing inputs
Prefer MockMvc for endpoint-focused tests.
Controller tests must not:
- normalize broken contracts
- ignore response wrapper rules
- assume raw entity exposure
Service Test Rule
Service tests should verify:
- business rule enforcement
- branching behavior
- state transitions
- conflict handling
- interactions with mapper or collaborators where meaningful
- auth/business logic in service layer when applicable
Use mocks for focused business logic tests when practical.
Use integration-style tests when the logic depends heavily on real persistence behavior.
Mapper / Persistence Test Rule
Write mapper or persistence integration tests when:
- custom queries matter
- filtering/search behavior matters
- soft delete behavior matters
- unique constraints or ownership relations matter
- pagination/query semantics are easy to break
Do not generate mapper tests for trivial persistence cases unless they are important.
Response Wrapper Rule
All successful API tests must assert:
{
"code": 0,
"message": "success",
"data": {}
}
Do not write tests as if the payload were the raw top-level response body.
Pagination Rule
Paginated backend API tests must assert:
{
"code": 0,
"message": "success",
"data": {
"content": [],
"page": 1,
"size": 10,
"total": 100
}
}
Tests must verify:
contentpagesizetotal
inside data.
Validation Test Rule
Request DTO / validation tests should cover as appropriate:
- required fields missing
- blank strings
- invalid sizes
- invalid ranges
- invalid enum-like values
- malformed request bodies
Tests should confirm:
- failure status
- stable error handling behavior
- no accidental success on invalid input
Auth / Security Test Rule
When the module is auth or protected by authentication/authorization, backend tests should cover as appropriate:
- public endpoints accessible without auth
- protected endpoints reject missing auth
- protected endpoints reject invalid auth
- valid bearer auth grants access
- forbidden role/permission cases
- current-user resolution from authenticated context
- login/register/refresh/logout/me flows
- invalid credential handling
- missing or malformed
Authorization: Bearer <token>header
For stateless JWT systems:
- test bearer-token-based access
- do not design tests around server-side session login as the primary auth mode
Soft Delete Test Rule
If the module uses soft delete:
- verify delete action updates soft delete state
- verify soft-deleted rows do not appear in normal list/detail behavior
- verify repeated delete or missing-row behavior is handled correctly
Error Handling Test Rule
Backend tests should also cover important negative paths:
- not found
- forbidden
- unauthorized
- conflict/duplicate business field
- invalid input
- invalid state transition
Do not rely only on happy-path tests.
Naming Rule
Test class names should remain clear and module-aligned.
Examples:
AuthControllerTestAuthServiceTestUserMapperIntegrationTestArticleControllerPaginationTest
Avoid vague names like:
Test1ServiceTestsControllerSpec
Contract Safety Rule
OpenAPI is the API source of truth.
Backend tests must reinforce:
- path correctness
- method correctness
- request field correctness
- response field correctness
- wrapper correctness
- pagination correctness
- auth boundary correctness
Do not let tests drift away from OpenAPI.
Forbidden Patterns
- testing only implementation details while ignoring contract behavior
- assuming raw top-level responses
- writing auth tests around server-side sessions when the approved strategy is stateless JWT
- ignoring negative paths
- generating many low-value tests while missing critical business rules
- exposing internal persistence fields in response assertions unless OpenAPI requires them
Final Goal
Backend tests should be:
- practical
- contract-safe
- regression-resistant
- aligned with module bo
