TDD workflow for Spring Boot with JUnit 5, Mockito, MockMvc, Testcontainers, and JaCoCo. Covers unit, web, integration, and persistence tests. Use when adding features, fixing bugs, or refactoring.
How this skill is triggered — by the user, by Claude, or both
Slash command
/everything-claude-code:springboot-tddThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Spring Boot 服务的 TDD 指南,目标 80%+ 覆盖率(单元 + 集成)。
Spring Boot 服务的 TDD 指南,目标 80%+ 覆盖率(单元 + 集成)。
@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());
}
}
模式:
@ParameterizedTest 处理变体@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 将 JDBC URL 注入 Spring 上下文Maven 配置片段:
<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)以提高可读性jsonPathassertThatThrownBy(...)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 或 mvn verify./gradlew test jacocoTestReport记住:保持测试快速、隔离和确定性。测试行为,而非实现细节。
npx claudepluginhub aaione/everything-claude-code-zhGuides Spring Boot TDD workflow with JUnit 5, Mockito, MockMvc, Testcontainers, and JaCoCo for 80%+ coverage in unit, web, integration, and persistence tests. Use for new features, bug fixes, refactoring.
Guides Spring Boot TDD with JUnit 5, Mockito, MockMvc, Testcontainers, and JaCoCo. Use when adding features, fixing bugs, or refactoring.
Provides Spring Boot testing patterns for unit (Mockito), slice (@DataJpaTest/@WebMvcTest), integration (@SpringBootTest), and Testcontainers-based tests with JUnit 5. Use when writing @Test methods, @MockBean mocks, or test suites.