AgentSkillsCN

testing-patterns

使用JUnit 5、Mockito、RestAssured进行REST集成测试模式。在编写模拟外部依赖的集成测试、使用Testcontainers/H2进行数据库测试以及用原始JSON正文测试端点时使用此功能。

SKILL.md
--- frontmatter
name: testing-patterns
description: REST integration testing patterns with JUnit 5, Mockito, RestAssured. Use when writing integration tests that mock external dependencies, use Testcontainers/H2 for databases, and test endpoints with raw JSON bodies.

REST Integration Testing Patterns

Write integration tests for REST endpoints using Quarkus test framework.


Quick Start

Basic REST integration test with RestAssured:

java
@QuarkusTest
class OrderResourceTest {

    @Test
    void shouldCreateOrder() {
        given()
            .contentType(ContentType.JSON)
            .body("""
                {
                    "customerId": "cust-123",
                    "items": [{"productId": "prod-1", "quantity": 2}]
                }
                """)
        .when()
            .post("/orders")
        .then()
            .statusCode(201)
            .body("id", notNullValue())
            .body("status", equalTo("PENDING"));
    }
}

Core Patterns

REST Integration Tests

Use: Test full request/response cycle through REST layer
Stack: @QuarkusTest + RestAssured
Cookbook: rest-integration-tests.md

Mocking External Dependencies

Use: Isolate tests from external HTTP services, queues, etc.
Stack: @InjectMock with Mockito
Cookbook: mocking-dependencies.md

Database with Testcontainers

Use: Test against real PostgreSQL/MySQL
Stack: Quarkus DevServices or @Testcontainers
Cookbook: testcontainers-setup.md

Database with H2

Use: Fast in-memory alternative when container not needed
Stack: H2 with test profile
Cookbook: h2-setup.md

Raw JSON Request Bodies

Use: Test with exact JSON payloads (no object serialization)
Stack: Text blocks + JsonPath assertions
Cookbook: json-request-bodies.md

Test Structure & Lifecycle

Use: Organize tests with JUnit 5 features
Stack: @Nested, @BeforeEach, naming conventions
Cookbook: test-structure.md


Quick Reference

PatternWhen to UseKey Annotation/Tool
REST IntegrationTest endpoint behavior@QuarkusTest
Mock DependenciesIsolate from external services@InjectMock
TestcontainersNeed real database behaviorDevServices / Container
H2 In-MemoryFast tests, simple queries%test profile
Raw JSON BodiesExact payload controlText blocks
Nested Test ClassesGroup related scenarios@Nested

Cookbook Index

Core Testing: REST Integration Tests · Test Structure

Dependencies: Mocking Dependencies

Database: Testcontainers Setup · H2 Setup

Request/Response: JSON Request Bodies