帮助你为 Spring Boot 项目实施测试驱动开发,覆盖新增、修复与重构场景
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "springboot-tdd" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/skills/springboot-tdd/SKILL.md 2. 保存为 ~/.claude/skills/springboot-tdd/SKILL.md 3. 装好后重载技能,告诉我可以用了
请用 TDD 方式为 Spring Boot 新增一个用户注册接口:先写失败的 JUnit 5 与 MockMvc 测试,再实现最小可用代码,使用 Mockito 模拟依赖,并补充关键边界条件测试。
一套按 TDD 步骤组织的测试与实现代码示例,包含接口、服务及边界条件测试。
这里有一个 Spring Boot 缺陷:订单取消后库存没有回滚。请先编写能复现问题的失败测试,再给出修复代码,并说明如何用 Mockito 或 Testcontainers 验证修复没有引入副作用。
包含缺陷复现测试、修复实现和验证方案的回归测试开发方案。
我想重构 Spring Boot 的订单服务,拆分过大的业务方法。请基于现有逻辑设计单元测试、集成测试和 JaCoCo 覆盖率目标,确保重构前后行为一致。
一份面向重构的测试方案,含测试分层建议、覆盖率目标与示例代码结构。
TDD guidance for Spring Boot services with 80%+ coverage (unit + integration).
@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());
}
}
Patterns:
@ParameterizedTest for variants@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());
}
}
@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());
}
}
@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();
}
}
@DynamicPropertySource to inject JDBC URLs into Spring contextMaven snippet:
<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>
assertThat) for readabilityjsonPathassertThatThrownBy(...)class MarketBuilder {
private String name = "Test";
MarketBuilder withName(String name) { this.name = name; return this; }
Market build() { return new Market(null, name, MarketStatus.ACTIVE); }
}
mvn -T 4 test or mvn verify./gradlew test jacocoTestReportRemember: Keep tests fast, isolated, and deterministic. Test behavior, not implementation details.
帮助开发者为代码代理配置性能优化、安全防护与研究优先工作流。
提供数据库迁移、回滚与零停机发布的最佳实践指导,适用于多种 ORM 与 SQL 数据库。
通过双评审智能体对结果进行对抗式校验,提升输出发布前的可靠性
帮助你掌握地道 Rust 模式、所有权与并发实践,编写安全高性能应用。
基于 C++ Core Guidelines 编写、审查并重构更安全现代的 C++ 代码。
为 Claude Code 会话提供系统化校验流程,帮助检查结果正确性与质量。
帮助你为 Spring Boot 项目实施测试驱动开发、修复缺陷并安全重构。
帮助你用 TDD 为 Spring Boot 编写、验证并重构高质量后端代码。
帮助开发者为 Quarkus 3.x 服务实施测试驱动开发、修复缺陷并安全重构。
为 Quarkus 3.x 项目提供测试驱动开发方案,支持功能开发、修复与重构。
帮助开发者用 Quarkus 3.x 实践测试驱动开发与重构验证。
用测试驱动开发流程编写新功能、修复缺陷并安全重构代码。