Skip to content

a-pavithraa/springboot-skills-marketplace / plugins/springboot-architecture/skills/writing-springboot-tests

writing-springboot-tests

Verified

Use when writing or adding tests or integration tests for Spring Boot code at a service's edges: HTTP clients that call other services (WireMock: 4xx and 5xx responses, timeouts, retries), Kafka or RabbitMQ listeners (redelivery, poison messages, dead-letter topics), events published to other services, or full-API tests against real databases with Testcontainers; domain logic whose rules must hold for every input (calculations, value objects, state transitions, parsing and formatting), tested as properties over generated inputs; also when a Boot 4 test needs the right imports, slices or container wiring. Supplies verified Boot 4.1 templates and the framework traps that make such tests pass without proving anything. Not for reviewing existing tests (see code-reviewer) or Boot 3 to 4 test migration (see springboot-migration).

assets/testcontainers-configuration.java

View on GitHub
package {{PACKAGE}};

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.postgresql.PostgreSQLContainer;

import static org.testcontainers.utility.DockerImageName.parse;

/**
 * The ONE place the test containers are declared. Every persistence and integration test imports it with
 * {@code @Import(TestcontainersConfiguration.class)}: the same import keeps one cached Spring context and one
 * running container for the whole test run.
 *
 * Use the database version production runs. @ServiceConnection wires spring.datasource.* for you, so don't
 * also set those properties.
 *
 * Boot 4 dependencies (test scope): spring-boot-testcontainers, org.testcontainers:testcontainers-junit-jupiter,
 * org.testcontainers:testcontainers-postgresql. Versions come from the Boot BOM.
 */
@TestConfiguration(proxyBeanMethods = false)
public class TestcontainersConfiguration {

    @Bean
    @ServiceConnection
    PostgreSQLContainer postgres() {
        return new PostgreSQLContainer(parse("postgres:18-alpine"));
    }

    // Kafka: org.testcontainers.kafka.KafkaContainer (not the old org.testcontainers.containers one).
    // @ServiceConnection sets spring.kafka.bootstrap-servers. Needs org.testcontainers:testcontainers-kafka.
    // @Bean
    // @ServiceConnection
    // KafkaContainer kafka() {
    //     return new KafkaContainer(parse("apache/kafka-native:4.1.1"));
    // }

    // Redis has no dedicated container class: a GenericContainer plus a name hint.
    // @Bean
    // @ServiceConnection(name = "redis")
    // GenericContainer<?> redis() {
    //     return new GenericContainer<>(parse("redis:8-alpine")).withExposedPorts(6379);
    // }

    // A container Boot has no connection factory for: register its mapped address lazily.
    // Fixed settings (feature flags, ddl-auto) belong in application-test.properties, not here.
    // @Bean
    // GenericContainer<?> mailpit() {
    //     return new GenericContainer<>(parse("axllent/mailpit:v1.27")).withExposedPorts(1025);
    // }
    //
    // @Bean
    // DynamicPropertyRegistrar mailProperties(GenericContainer<?> mailpit) {
    //     return registry -> {
    //         registry.add("spring.mail.host", mailpit::getHost);
    //         registry.add("spring.mail.port", () -> mailpit.getMappedPort(1025));
    //     };
    // }
}
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall