spring-testing

Coding rules for testing Spring Boot applications, a Java framework for building web services. They cover API tests, smaller unit tests, and Testcontainers, which runs real supporting services such as a database in temporary containers.

In plain words
What is it for?
Use them when creating or changing Spring Boot tests with JUnit 5, jOOQ, or Testcontainers. They also specify suitable Spring test slices and mocking patterns.
Why use it?
They give developers a consistent way to test controllers, services, repositories, and database-related behavior. They emphasize checking the application through its API before relying on isolated mocks.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/e-gov/cursor-prompts/spring-testing
Clone the repo
git clone --depth 1 https://github.com/e-gov/cursor-prompts
Per session 2,802 This file is loaded in full into every session.
When invoked 2,802 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.02802 $0.02802
Opus 5 $0.01401 $0.01401
Sonnet 5 $0.00560 $0.00560
Haiku 4.5 $0.00280 $0.00280

Measured 2d ago against content hash 54c3485eca41, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

spring-testing scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules/java-spring-boot/spring-testing.mdc · 329 lines

How it starts

The opening of the file, as written. The whole thing — 329 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Spring Boot Testing Standards

Context

  • Apply when creating or modifying Spring Boot test classes
  • For testing controllers, services, repositories, and integration scenarios
  • When using Spring Boot Test framework with JUnit 5
  • For both unit tests and integration tests with TestContainers
  • When using jOOQ for database access and complex mocking scenarios
  • Follow integration-first testing approach: comprehensive API tests, then focused unit tests
  • Note: Some examples show Spring Boot 4-specific patterns (marked with "Spring Boot 4+"). For Spring Boot 3.5, use PostgreSQLContainer<?> (with generic) and TestRestTemplate from org.springframework.boot.test.web.client.TestRestTemplate without @AutoConfigureTestRestTemplate

Requirements

  1. Test Strategy: Prefer integration tests against the API first, then unit tests with mocks for specific logic testing
  2. Test Structure: Use appropriate Spring Boot test slices (@WebMvcTest, @DataJpaTest, @JsonTest, etc.)
  3. Integration Tests: Use @SpringBootTest with TestContainers for full integration testing
  4. Mocking: Use @Mock + @InjectMocks for unit tests. Use @MockitoBean in Spring test slices when bean replacement is necessary
  5. Test Profiles: Use @ActiveProfiles("test") for test-specific configuration
  6. Database Testing: Use TestContainers with PostgreSQL for realistic database testing
  7. Web Layer Testing: Use MockMvc for controller testing with proper content type assertions
  8. Base Test Classes: Use inheritance for shared test infrastructure and common setup
  9. TestContainers Configuration: Use singleton pattern with @ServiceConnection for container reuse
  10. External HTTP APIs: Use WireMock for mocking external HTTP dependencies in tests

Examples

private static final Logger log = LoggerFactory.getLogger(TestcontainersConfiguration.class);
private static final DockerImageName POSTGRES_IMAGE = DockerImageName.parse("postgres:15")
        .asCompatibleSubstituteFor("postgres");

// Spring Boot 4+ / Testcontainers 2.x: No generic type parameter
// Spring Boot 3.5: Use PostgreSQLContainer<?>
private static final PostgreSQLContainer POSTGRES_CONTAINER;

static {
    log.info("Initializing PostgreSQL Testcontainer with reuse enabled...");
    POSTGRES_CONTAINER = new PostgreSQLContainer(POSTGRES_IMAGE)
            .withDatabaseName("bookstore_test")
            .withUsername("test")
            .withPassword("test")
            .withReuse(true)
            .withStartupTimeout(Duration.ofSeconds(60))
            .withConnectTimeoutSeconds(60);

    POSTGRES_CONTAINER.start();

    // Add shutdown hook for robustness
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        if (POSTGRES_CONTAINER != null && POSTGRES_CONTAINER.isRunning()) {
            POSTGRES_CONTAINER.close();
        }
    }));
}

@Bean
@ServiceConnection
// Spring Boot 4+ / Testcontainers 2.x: No generic type parameter
// Spring Boot 3.5: Use PostgreSQLContainer<?>
public PostgreSQLContainer postgresContainer() {
    return POSTGRES_CONTAINER;
}

@DynamicPropertySource
public static void registerDynamicProperties(DynamicPropertyRegistry registry) {
    registry.add("spring.datasource.url", POSTGRES_CONTAINER::getJdbcUrl);
    registry.add("spring.datasource.username", POSTGRES_CONTAINER::getUsername);
    registry.add("spring.datasource.password", POSTGRES_CONTAINER::getPassword);
    registry.add("spring.datasource.driver-class-name", POSTGRES_CONTAINER::getDriverClassName);
}

// Spring Boot 4+ / Testcontainers 2.x: No generic type parameter
// Spring Boot 3.5: Use PostgreSQLContainer<?>
public static PostgreSQLContainer getPostgresContainer() {
    return POSTGRES_CONTAINER;
}

}

</example>

Read the full file on GitHub · 329 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 329 lines · 2,802 tokens per session scan A 54c3485eca41

Subscribe to this mod's changes

spring-testing is a cursor rule published in the GitHub repository e-gov/cursor-prompts (34 stars, last pushed 4mo ago), licensed MIT. It adds 2,802 tokens to every session, about $0.0140 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.