搜索

PythonReactSEOPDF
GitHub Skill

java-testing

Java testing guidelines using Spock Framework and Groovy. Covers test structure, mocking patterns, persistence testing with Testcontainers, gRPC testing, and Spring Boot test configurations. Use when writing or reviewing tests.

bitsoex☆ 35 仓库 Stars30 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

Java Testing

Guidelines for writing tests in Java projects using the Spock Framework and Groovy.

When to use this skill

  • Writing new tests
  • Reviewing test code
  • Setting up test infrastructure
  • Configuring Testcontainers
  • Mocking dependencies in Spring Boot tests

Skill Contents

Sections
Available Resources

📚 references/ - Detailed documentation


Quick Start

1. Test Structure
def "should do Y when X"() {
    given:
        // create test data
    when:
        // execute process
    then:
        // assert output
    where: // use test table if suited
}
2. Test Naming
  • Test files: <ClassName>Spec.groovy
  • Test methods: "should [expected behavior] when [condition]"

Key Patterns

Pattern When to Use
Spock Mock Unit tests without Spring context
@SpringBean Spring integration tests with mocks
@ServiceConnection Testcontainers database connection
InProcessServer gRPC client/server testing
Data Tables Multiple test cases with same structure

Mocking Patterns

Unit Tests (No Spring)
class SomeSpec extends Specification {
    SomeInterface someInterface = Mock()

    def "test method"() {
        given:
            def underTest = new ClassUnderTest(someInterface)
        when:
            // call method
        then:
            1 * someInterface.someMethod("value") >> responseClass
    }
}
Spring Integration Tests
@SpringBootTest
class SomeSpec extends Specification {
    @SpringBean
    SomeInterface someInterface = Mock()

    def "test method"() {
        when:
            // call method
        then:
            1 * someInterface.someMethod(_) >> { throw new RuntimeException() }
    }
}

Persistence Testing

Use Testcontainers with @ServiceConnection for database tests:

@Configuration
class IntegrationTestConfiguration {
    @Bean
    @ServiceConnection
    PostgreSQLContainer<?> postgreSQLContainer() {
        return new PostgreSQLContainer<>("postgres:14-alpine")
                .withDatabaseName("test_db")
    }
}

References

Reference Description
references/spock-patterns.md Advanced Spock patterns
references/testcontainers-setup.md Testcontainers configuration
references/grpc-testing.md gRPC test patterns

Related Rules

  • .cursor/rules/java-testing-guidelines.mdc - Full testing reference
  • .cursor/rules/java-flyway-migrations.mdc - Flyway for test schema

Related Skills

Skill Purpose
java-coverage JaCoCo test coverage
gradle-standards Test dependency bundles

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-01-20
索引更新时间
2026-01-28
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!