Guides 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.
npx claudepluginhub xu-xiang/everything-claude-code-zhThis skill uses the workspace's default tool permissions.
针对具有 80% 以上覆盖率(单元测试 + 集成测试)的 Spring Boot 服务的 TDD 指南。
Guides TDD workflow for Spring Boot services using JUnit 5, Mockito, MockMvc, Testcontainers, and JaCoCo. For new features, bug fixes, refactoring with 80%+ coverage.
Guides TDD for Spring Boot with JUnit 5, Mockito, MockMvc, Testcontainers, JaCoCo. Covers unit, web, integration, persistence tests for features, bugs, refactors.
Guides Spring Boot 4 testing with JUnit 6, AssertJ, test slices (@WebMvcTest, @DataJpaTest), Testcontainers, MockMvcTester, and RestTestClient for controllers, repos, and services.
Share bugs, ideas, or general feedback.
针对具有 80% 以上覆盖率(单元测试 + 集成测试)的 Spring Boot 服务的 TDD 指南。
@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记住:保持测试快速、隔离且具有确定性。测试行为,而不是实现细节。