Springboot verification

From aaione/everything-claude-code-zh · View interactive version

Springboot verification is an AI agent skill from the GitHub repository aaione/everything-claude-code-zh. Spring Boot 项目的验证循环:构建、静态分析、带覆盖率的测试、安全扫描和发布或 PR 前的差异审查。

How to use this skill

Skill file

Spring Boot 验证循环

在 PR 之前、重大更改之后和部署前运行。

何时激活

阶段 1:构建

mvn -T 4 clean verify -DskipTests
# 或
./gradlew clean assemble -x test

如果构建失败,停止并修复。

阶段 2:静态分析

Maven(常用插件):

mvn -T 4 spotbugs:check pmd:check checkstyle:check

Gradle(如果已配置):

./gradlew checkstyleMain pmdMain spotbugsMain

阶段 3:测试 + 覆盖率

mvn -T 4 test
mvn jacoco:report   # 验证 80%+ 覆盖率
# 或
./gradlew test jacocoTestReport

报告:

单元测试

使用模拟依赖隔离测试服务逻辑:

@ExtendWith(MockitoExtension.class)
class UserServiceTest {

  @Mock private UserRepository userRepository;
  @InjectMocks private UserService userService;

  @Test
  void createUser_validInput_returnsUser() {
    var dto = new CreateUserDto("Alice", "alice@example.com");
    var expected = new User(1L, "Alice", "alice@example.com");
    when(userRepository.save(any(User.class))).thenReturn(expected);

    var result = userService.create(dto);

    assertThat(result.name()).isEqualTo("Alice");
    verify(userRepository).save(any(User.class));
  }

  @Test
  void createUser_duplicateEmail_throwsException() {
    var dto = new CreateUserDto("Alice", "existing@example.com");
    when(userRepository.existsByEmail(dto.email())).thenReturn(true);

    assertThatThrownBy(() -> userService.create(dto))
        .isInstanceOf(DuplicateEmailException.class);
  }
}

使用 Testcontainers 的集成测试

使用真实数据库而非 H2:

@SpringBootTest
@Testcontainers
class UserRepositoryIntegrationTest {

  @Container
  static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine")
      .withDatabaseName("testdb");

  @DynamicPropertySource
  static void configureProperties(DynamicPropertyRegistry registry) {
    registry.add("spring.datasource.url", postgres::getJdbcUrl);
    registry.add("spring.datasource.username", postgres::getUsername);
    registry.add("spring.datasource.password", postgres::getPassword);
  }

  @Autowired private UserRepository userRepository;

  @Test
  void findByEmail_existingUser_returnsUser() {
    userRepository.save(new User("Alice", "alice@example.com"));

    var found = userRepository.findByEmail("alice@example.com");

    assertThat(found).isPresent();
    assertThat(found.get().getName()).isEqualTo("Alice");
  }
}

使用 MockMvc 的 API 测试

使用完整 Spring 上下文测试控制器层:

@WebMvcTest(UserController.class)
class UserControllerTest {

  @Autowired private MockMvc mockMvc;
  @MockBean private UserService userService;

  @Test
  void createUser_validInput_returns201() throws Exception {
    var user = new UserDto(1L, "Alice", "alice@example.com");
    when(userService.create(any())).thenReturn(user);

    mockMvc.perform(post("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content("""
                {"name": "Alice", "email": "alice@example.com"}
                """))
        .andExpect(status().isCreated())
        .andExpect(jsonPath("$.name").value("Alice"));
  }

  @Test
  void createUser_invalidEmail_returns400() throws Exception {
    mockMvc.perform(post("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content("""
                {"name": "Alice", "email": "not-an-email"}
                """))
        .andExpect(status().isBadRequest());
  }
}

阶段 4:安全扫描

# 依赖 CVE 检查
mvn org.owasp:dependency-check-maven:check
# 或
./gradlew dependencyCheckAnalyze

# 源代码中的密钥
grep -rn "password\s*=\s*\"" src/ --include="*.java" --include="*.yml" --include="*.properties"
grep -rn "sk-\|api_key\|secret" src/ --include="*.java" --include="*.yml"

# 密钥(git 历史)
git secrets --scan  # 如果已配置

常见安全发现

# 检查 System.out.println(改用 logger)
grep -rn "System\.out\.print" src/main/ --include="*.java"

# 检查响应中的原始异常消息
grep -rn "e\.getMessage()" src/main/ --include="*.java"

# 检查通配符 CORS
grep -rn "allowedOrigins.*\*" src/main/ --include="*.java"

阶段 5:Lint/格式化(可选门控)

mvn spotless:apply   # 如果使用 Spotless 插件
./gradlew spotlessApply

阶段 6:差异审查

git diff --stat
git diff

检查清单:

输出模板

验证报告
===================
构建:     [通过/失败]
静态分析:    [通过/失败](spotbugs/pmd/checkstyle)
测试:     [通过/失败](X/Y 通过,Z% 覆盖率)
安全:  [通过/失败](CVE 发现:N)
差异:      [X 个文件变更]

总体:   [就绪 / 未就绪]

待修复问题:
1. ...
2. ...

持续模式

记住:快速反馈胜过延迟意外。保持门控严格 — 在生产系统中将警告视为缺陷。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.