home / skills / emvnuel / skill.md / testing-patterns

testing-patterns skill

/testing-patterns

This skill helps you implement robust REST integration tests using Quarkus, RestAssured, Mockito, and Testcontainers for reliable end-to-end validation.

npx playbooks add skill emvnuel/skill.md --skill testing-patterns

Review the files below or copy the command above to add this skill to your agents.

Files (7)
SKILL.md
3.3 KB
---
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](./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](./cookbook/mocking-dependencies.md)

### Database with Testcontainers

**Use**: Test against real PostgreSQL/MySQL  
**Stack**: Quarkus DevServices or `@Testcontainers`  
**Cookbook**: [testcontainers-setup.md](./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](./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](./cookbook/json-request-bodies.md)

### Test Structure & Lifecycle

**Use**: Organize tests with JUnit 5 features  
**Stack**: `@Nested`, `@BeforeEach`, naming conventions  
**Cookbook**: [test-structure.md](./cookbook/test-structure.md)

---

## Quick Reference

| Pattern             | When to Use                    | Key Annotation/Tool     |
| ------------------- | ------------------------------ | ----------------------- |
| REST Integration    | Test endpoint behavior         | `@QuarkusTest`          |
| Mock Dependencies   | Isolate from external services | `@InjectMock`           |
| Testcontainers      | Need real database behavior    | DevServices / Container |
| H2 In-Memory        | Fast tests, simple queries     | `%test` profile         |
| Raw JSON Bodies     | Exact payload control          | Text blocks             |
| Nested Test Classes | Group related scenarios        | `@Nested`               |

---

## Cookbook Index

**Core Testing**: [REST Integration Tests](./cookbook/rest-integration-tests.md) · [Test Structure](./cookbook/test-structure.md)

**Dependencies**: [Mocking Dependencies](./cookbook/mocking-dependencies.md)

**Database**: [Testcontainers Setup](./cookbook/testcontainers-setup.md) · [H2 Setup](./cookbook/h2-setup.md)

**Request/Response**: [JSON Request Bodies](./cookbook/json-request-bodies.md)

Overview

This skill provides practical patterns for REST integration testing using JUnit 5, Mockito, and RestAssured. It focuses on testing endpoints end-to-end while selectively mocking external dependencies and choosing between Testcontainers or H2 for databases. The guidance emphasizes raw JSON payload testing and clean test structure for reliable, fast suites.

How this skill works

The skill outlines a set of repeatable patterns: run full request/response checks with RestAssured under a Quarkus-style test context, inject Mockito mocks to isolate external services, and run database-backed tests either against real containers or an in-memory H2 profile. It shows using text-block JSON bodies and JsonPath assertions for exact payload control and recommends JUnit 5 lifecycle and nested tests to organize scenarios.

When to use it

  • Verify REST endpoint behavior including status codes and response bodies
  • Isolate service under test from external HTTP services, message queues, or clients
  • Validate real database interactions and migrations with Testcontainers
  • Run fast, lightweight tests using an H2 in-memory profile
  • Assert exact JSON payloads and response shapes using raw JSON and JsonPath

Best practices

  • Prefer RestAssured within a managed test context to exercise the full HTTP layer
  • Use @InjectMock (Mockito) to replace external integrations; assert interactions where appropriate
  • Use Testcontainers or DevServices when you need real database semantics and isolation
  • Use H2 with a dedicated test profile for fast feedback when full DB fidelity is unnecessary
  • Write raw JSON text-blocks for request bodies when you need precise payload control and avoid serialization differences
  • Organize tests with JUnit 5 @Nested classes and @BeforeEach/@AfterEach to keep setup focused and readable

Example use cases

  • Create an order endpoint test that posts raw JSON and asserts created status and response fields
  • Mock an external payment gateway to simulate success and failure flows without network calls
  • Run integration tests against a PostgreSQL container to validate queries, constraints, and migrations
  • Switch to H2 for CI jobs or developer runs where speed matters over exact DB behavior
  • Group positive and negative endpoint scenarios in nested test classes for clearer reports

FAQ

When should I choose Testcontainers over H2?

Choose Testcontainers when you need real DB behavior, types, or extensions that H2 cannot emulate; use H2 for faster, simpler tests where exact DB semantics are not critical.

How do I test exact JSON shapes?

Send raw JSON using text blocks in your test and assert response fields with JsonPath; this avoids differences caused by object serialization.