springboot-tdd

使用 JUnit 5, Mockito, MockMvc, Testcontainers 和 JaCoCo 进行 Spring Boot 的测试驱动开发(TDD)。适用于添加新功能、修复 bug 或重构场景。

351 stars

Best use case

springboot-tdd is best used when you need a repeatable AI agent workflow instead of a one-off prompt. It is especially useful for teams working in multi. 使用 JUnit 5, Mockito, MockMvc, Testcontainers 和 JaCoCo 进行 Spring Boot 的测试驱动开发(TDD)。适用于添加新功能、修复 bug 或重构场景。

使用 JUnit 5, Mockito, MockMvc, Testcontainers 和 JaCoCo 进行 Spring Boot 的测试驱动开发(TDD)。适用于添加新功能、修复 bug 或重构场景。

Users should expect a more consistent workflow output, faster repeated execution, and less time spent rewriting prompts from scratch.

Practical example

Example input

Use the "springboot-tdd" skill to help with this workflow task. Context: 使用 JUnit 5, Mockito, MockMvc, Testcontainers 和 JaCoCo 进行 Spring Boot 的测试驱动开发(TDD)。适用于添加新功能、修复 bug 或重构场景。

Example output

A structured workflow result with clearer steps, more consistent formatting, and an output that is easier to reuse in the next run.

When to use this skill

  • Use this skill when you want a reusable workflow rather than writing the same prompt again and again.

When not to use this skill

  • Do not use this when you only need a one-off answer and do not need a reusable workflow.
  • Do not use it if you cannot install or maintain the related files, repository context, or supporting tools.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/springboot-tdd/SKILL.md --create-dirs "https://raw.githubusercontent.com/xu-xiang/everything-claude-code-zh/main/docs/ja-JP/skills/springboot-tdd/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/springboot-tdd/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How springboot-tdd Compares

Feature / Agentspringboot-tddStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

使用 JUnit 5, Mockito, MockMvc, Testcontainers 和 JaCoCo 进行 Spring Boot 的测试驱动开发(TDD)。适用于添加新功能、修复 bug 或重构场景。

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Spring Boot TDD 工作流 (Spring Boot TDD Workflow)

为 Spring Boot 服务提供测试驱动开发(TDD)指南,要求覆盖率达到 80% 以上(单元测试 + 集成测试)。

## 何时使用

- 新功能或新端点(Endpoint)
- Bug 修复或重构
- 添加数据访问逻辑或安全规则

## 工作流 (Workflow)

1) 先写测试(且测试应当失败)
2) 实现能让测试通过的最小化代码
3) 在保持测试通过(Green)的状态下进行重构
4) 强制执行覆盖率检查(JaCoCo)

## 单元测试 (Unit Testing - JUnit 5 + Mockito)

```java
@ExtendWith(MockitoExtension.class)
class MarketServiceTest {
  @Mock MarketRepository repo;
  @InjectMocks MarketService service;

  @Test
  void createsMarket() {
    CreateMarketRequest req = new CreateMarketRequest("name", "desc", Instant.now(), List.of("cat"));
    when(repo.save(any())).thenAnswer(inv -> inv.getArgument(0));

    Market result = service.create(req);

    assertThat(result.name()).isEqualTo("name");
    verify(repo).save(any());
  }
}
```

模式:
- Arrange-Act-Assert (准备-执行-断言)
- 避免使用部分 Mock(Partial Mock),优先使用显式打桩(Stubbing)
- 针对多种变体情况使用 `@ParameterizedTest`

## Web 层测试 (Web Layer Testing - MockMvc)

```java
@WebMvcTest(MarketController.class)
class MarketControllerTest {
  @Autowired MockMvc mockMvc;
  @MockBean MarketService marketService;

  @Test
  void returnsMarkets() throws Exception {
    when(marketService.list(any())).thenReturn(Page.empty());

    mockMvc.perform(get("/api/markets"))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.content").isArray());
  }
}
```

## 集成测试 (Integration Testing - SpringBootTest)

```java
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
class MarketIntegrationTest {
  @Autowired MockMvc mockMvc;

  @Test
  void createsMarket() throws Exception {
    mockMvc.perform(post("/api/markets")
        .contentType(MediaType.APPLICATION_JSON)
        .content("""
          {"name":"Test","description":"Desc","endDate":"2030-01-01T00:00:00Z","categories":["general"]}
        """))
      .andExpect(status().isCreated());
  }
}
```

## 持久化测试 (Persistence Testing - DataJpaTest)

```java
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Import(TestContainersConfig.class)
class MarketRepositoryTest {
  @Autowired MarketRepository repo;

  @Test
  void savesAndFinds() {
    MarketEntity entity = new MarketEntity();
    entity.setName("Test");
    repo.save(entity);

    Optional<MarketEntity> found = repo.findByName("Test");
    assertThat(found).isPresent();
  }
}
```

## Testcontainers

- 使用可复用的 Postgres/Redis 容器以反映生产环境
- 通过 `@DynamicPropertySource` 将 JDBC URL 注入 Spring 上下文 (Context)

## 覆盖率 (Coverage - JaCoCo)

Maven 代码片段:
```xml
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.14</version>
  <executions>
    <execution>
      <goals><goal>prepare-agent</goal></goals>
    </execution>
    <execution>
      <id>report</id>
      <phase>verify</phase>
      <goals><goal>report</goal></goals>
    </execution>
  </executions>
</plugin>
```

## 断言 (Assertions)

- 为了可读性,优先使用 AssertJ (`assertThat`)
- 对于 JSON 响应使用 `jsonPath`
- 对于异常:`assertThatThrownBy(...)`

## 测试数据生成器 (Test Data Builder)

```java
class MarketBuilder {
  private String name = "Test";
  MarketBuilder withName(String name) { this.name = name; return this; }
  Market build() { return new Market(null, name, MarketStatus.ACTIVE); }
}
```

## CI 命令 (CI Commands)

- Maven: `mvn -T 4 test` 或 `mvn verify`
- Gradle: `./gradlew test jacocoTestReport`

**请记住**:保持测试的高速、隔离和确定性。测试行为而非实现细节。

Related Skills

springboot-verification

351
from xu-xiang/everything-claude-code-zh

Spring Boot 项目的验证循环:构建、静态分析、带有覆盖率的测试、安全扫描以及发布或 PR 前的差异审查。

springboot-security

351
from xu-xiang/everything-claude-code-zh

Spring Boot 服务的 Spring Security 身份验证/授权、验证、CSRF、密钥、响应头、速率限制和依赖项安全最佳实践。

springboot-patterns

351
from xu-xiang/everything-claude-code-zh

Spring Boot 架构模式、REST API 设计、分层服务、数据访问、缓存、异步处理与日志。适用于 Java Spring Boot 后端开发。

plankton-code-quality

351
from xu-xiang/everything-claude-code-zh

使用 Plankton 实现编写时代码质量强制执行 —— 通过钩子在每次文件编辑时进行自动格式化、代码检查,并由 Claude 驱动自动修复。

autonomous-loops

351
from xu-xiang/everything-claude-code-zh

自主运行 Claude Code 循环的模式与架构 —— 从简单的顺序流水线到 RFC 驱动的多智能体 DAG 系统。

visa-doc-translate

351
from xu-xiang/everything-claude-code-zh

将签证申请文件(图片)翻译成英文,并创建包含原文和译文的双语PDF

swiftui-patterns

351
from xu-xiang/everything-claude-code-zh

SwiftUI 架构模式,使用 @Observable 进行状态管理,视图组合,导航,性能优化,以及现代 iOS/macOS UI 最佳实践。

swift-protocol-di-testing

351
from xu-xiang/everything-claude-code-zh

基于协议的依赖注入,用于可测试的Swift代码——使用聚焦协议和Swift Testing模拟文件系统、网络和外部API。

swift-concurrency-6-2

351
from xu-xiang/everything-claude-code-zh

Swift 6.2 可接近的并发性 — 默认单线程,@concurrent 用于显式后台卸载,隔离一致性用于主 actor 类型。

swift-actor-persistence

351
from xu-xiang/everything-claude-code-zh

在 Swift 中使用 actor 实现线程安全的数据持久化——基于内存缓存与文件支持的存储,通过设计消除数据竞争。

skill-stocktake

351
from xu-xiang/everything-claude-code-zh

用于审计Claude技能和命令的质量。支持快速扫描(仅变更技能)和全面盘点模式,采用顺序子代理批量评估。

search-first

351
from xu-xiang/everything-claude-code-zh

研究优先于编码的工作流程。在编写自定义代码之前,搜索现有的工具、库和模式。调用研究员代理。