← 返回列表
全部 Skill›工具 Tools›backend-testing-guideline

backend-testing-guideline

作者 @findsun-vn · 语言 en
Guide Spring Boot backend test generation for module-based architecture, OpenAPI contract safety, and practical business coverage.
工具 Tools生产力工具通用待翻译
最近源活动
—
检测 SKILL.md 语言
en
星标 ★
0
分支 Fork
0
安装选项
提示
指令
使用 AI 助手安装
将下面的提示复制到你正在使用的 AI 助手中。
下载完整 Skill 目录(含全部配套文件)。 下载压缩包 ↓
SKILL.md 原文预览

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:

  1. critical business rules
  2. API contract correctness
  3. validation behavior
  4. security/auth boundaries
  5. pagination and wrapper correctness
  6. soft delete behavior
  7. 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:

  • content
  • page
  • size
  • total

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:

  • AuthControllerTest
  • AuthServiceTest
  • UserMapperIntegrationTest
  • ArticleControllerPaginationTest

Avoid vague names like:

  • Test1
  • ServiceTests
  • ControllerSpec

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
评论
Ffindsun-vn
findsun-vn
findsun-vn/E-Commerce
查看 GitHub 仓库 查看作者的其他 Skill
来源信息
作者@findsun-vn
所属大类工具 Tools / 生产力工具
国内可用性通用
最近源活动—
风险提示无
提示:风险标签为程序静态扫描结果,仅作提示,不代表安全认证。安装前请阅读 SKILL.md 及其引用的脚本、权限,自行判断是否可信。