mirror of
https://github.com/sweetwisdom/everything-claude-code-zh.git
synced 2026-03-22 06:20:10 +00:00
chore: sync with upstream e7cb442 + update zh translations
This commit is contained in:
@@ -1,26 +1,26 @@
|
||||
---
|
||||
name: springboot-tdd
|
||||
description: Test-driven development for Spring Boot using JUnit 5, Mockito, MockMvc, Testcontainers, and JaCoCo. Use when adding features, fixing bugs, or refactoring.
|
||||
description: 使用 JUnit 5、Mockito、MockMvc、Testcontainers 和 JaCoCo 进行 Spring Boot 的测试驱动开发(TDD)。在添加功能、修复 Bug 或进行重构时使用。
|
||||
---
|
||||
|
||||
# Spring Boot TDD Workflow
|
||||
# Spring Boot 测试驱动开发(TDD)工作流
|
||||
|
||||
TDD guidance for Spring Boot services with 80%+ coverage (unit + integration).
|
||||
针对 Spring Boot 服务的 TDD 指南,要求 80% 以上的覆盖率(单元测试 + 集成测试)。
|
||||
|
||||
## When to Use
|
||||
## 适用场景
|
||||
|
||||
- New features or endpoints
|
||||
- Bug fixes or refactors
|
||||
- Adding data access logic or security rules
|
||||
- 开发新功能或端点(Endpoints)
|
||||
- 修复 Bug 或进行代码重构
|
||||
- 添加数据访问逻辑或安全规则
|
||||
|
||||
## Workflow
|
||||
## 工作流
|
||||
|
||||
1) Write tests first (they should fail)
|
||||
2) Implement minimal code to pass
|
||||
3) Refactor with tests green
|
||||
4) Enforce coverage (JaCoCo)
|
||||
1) 先写测试(测试应当失败)
|
||||
2) 实现最少量的代码以使测试通过
|
||||
3) 在测试通过(Green)的前提下进行重构
|
||||
4) 强制执行覆盖率检查(JaCoCo)
|
||||
|
||||
## Unit Tests (JUnit 5 + Mockito)
|
||||
## 单元测试(JUnit 5 + Mockito)
|
||||
|
||||
```java
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@@ -41,12 +41,12 @@ class MarketServiceTest {
|
||||
}
|
||||
```
|
||||
|
||||
Patterns:
|
||||
- Arrange-Act-Assert
|
||||
- Avoid partial mocks; prefer explicit stubbing
|
||||
- Use `@ParameterizedTest` for variants
|
||||
模式:
|
||||
- Arrange-Act-Assert(准备-执行-断言)
|
||||
- 避免部分打桩(Partial Mocks);优先使用显式桩函数(Stubbing)
|
||||
- 使用 `@ParameterizedTest` 处理多种变体场景
|
||||
|
||||
## Web Layer Tests (MockMvc)
|
||||
## Web 层测试(MockMvc)
|
||||
|
||||
```java
|
||||
@WebMvcTest(MarketController.class)
|
||||
@@ -65,7 +65,7 @@ class MarketControllerTest {
|
||||
}
|
||||
```
|
||||
|
||||
## Integration Tests (SpringBootTest)
|
||||
## 集成测试(SpringBootTest)
|
||||
|
||||
```java
|
||||
@SpringBootTest
|
||||
@@ -86,7 +86,7 @@ class MarketIntegrationTest {
|
||||
}
|
||||
```
|
||||
|
||||
## Persistence Tests (DataJpaTest)
|
||||
## 持久层测试(DataJpaTest)
|
||||
|
||||
```java
|
||||
@DataJpaTest
|
||||
@@ -109,12 +109,12 @@ class MarketRepositoryTest {
|
||||
|
||||
## Testcontainers
|
||||
|
||||
- Use reusable containers for Postgres/Redis to mirror production
|
||||
- Wire via `@DynamicPropertySource` to inject JDBC URLs into Spring context
|
||||
- 使用可重用的容器(如 Postgres/Redis)来模拟生产环境
|
||||
- 通过 `@DynamicPropertySource` 进行连接,将 JDBC URL 注入到 Spring 上下文中
|
||||
|
||||
## Coverage (JaCoCo)
|
||||
## 覆盖率(JaCoCo)
|
||||
|
||||
Maven snippet:
|
||||
Maven 配置片段:
|
||||
```xml
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
@@ -133,13 +133,13 @@ Maven snippet:
|
||||
</plugin>
|
||||
```
|
||||
|
||||
## Assertions
|
||||
## 断言(Assertions)
|
||||
|
||||
- Prefer AssertJ (`assertThat`) for readability
|
||||
- For JSON responses, use `jsonPath`
|
||||
- For exceptions: `assertThatThrownBy(...)`
|
||||
- 为了提高可读性,优先选择 AssertJ (`assertThat`)
|
||||
- 对于 JSON 响应,使用 `jsonPath`
|
||||
- 对于异常测试:`assertThatThrownBy(...)`
|
||||
|
||||
## Test Data Builders
|
||||
## 测试数据构建器(Test Data Builders)
|
||||
|
||||
```java
|
||||
class MarketBuilder {
|
||||
@@ -149,9 +149,9 @@ class MarketBuilder {
|
||||
}
|
||||
```
|
||||
|
||||
## CI Commands
|
||||
## CI 命令
|
||||
|
||||
- Maven: `mvn -T 4 test` or `mvn verify`
|
||||
- Gradle: `./gradlew test jacocoTestReport`
|
||||
- Maven:`mvn -T 4 test` 或 `mvn verify`
|
||||
- Gradle:`./gradlew test jacocoTestReport`
|
||||
|
||||
**Remember**: Keep tests fast, isolated, and deterministic. Test behavior, not implementation details.
|
||||
**记住**:保持测试快速、隔离且具有确定性。测试的是行为,而非实现细节。
|
||||
|
||||
Reference in New Issue
Block a user