giuseppe-trisciuoglio/developer-kit / plugins/developer-kit-java/skills/spring-boot-test-patterns
spring-boot-test-patterns
VerifiedProvides comprehensive testing patterns for Spring Boot applications covering unit, integration, slice, and container-based testing with JUnit 5, Mockito, Testcontainers, and performance optimization. Use when writing tests, @Test methods, @MockBean mocks, or implementing test suites for Spring Boot applications.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 9.1 KB |
| references/api-reference.md | markdown | 3.4 KB |
| references/best-practices.md | markdown | 6.7 KB |
| references/ci-cd-configuration.md | markdown | 4.0 KB |
| references/slice-testing.md | markdown | 6.8 KB |
| references/test-dependencies.md | markdown | 2.6 KB |
| references/testcontainers-setup.md | markdown | 3.9 KB |
| references/unit-testing.md | markdown | 5.4 KB |
| references/workflow-patterns.md | markdown | 9.1 KB |
references/testcontainers-setup.md
View on GitHub# Testcontainers Configuration
## Spring Boot 3.5+ `@ServiceConnection`
```java
@TestConfiguration
public class TestContainerConfig {
@Bean
@ServiceConnection
public PostgreSQLContainer<?> postgresContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:16-alpine"))
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");
}
@Bean
@ServiceConnection
public GenericContainer<?> redisContainer() {
return new GenericContainer<>(DockerImageName.parse("redis:7-alpine"))
.withExposedPorts(6379);
}
}
```
Apply with `@Import(TestContainerConfig.class)` on test classes.
## Traditional `@DynamicPropertySource`
```java
@Testcontainers
class UserServiceIntegrationTest {
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
DockerImageName.parse("postgres:16-alpine"))
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");
@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}
}
```
## Multiple Containers
```java
@Testcontainers
class MultiContainerIntegrationTest {
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
"postgres:16-alpine")
.withDatabaseName("testdb");
@Container
static GenericContainer<?> redis = new GenericContainer<>(
"redis:7-alpine")
.withExposedPorts(6379);
@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.redis.host", redis::getHost);
registry.add("spring.redis.port", redis::getFirstMappedPort);
}
}
```
## Container Reuse Strategy
```java
@Testcontainers(disableWithoutDocker = true)
class ContainerConfig {
static final PostgreSQLContainer<?> POSTGRES = new PostgreSQLContainer<>(
DockerImageName.parse("postgres:16-alpine"))
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test")
.withReuse(true);
@BeforeAll
static void startAll() {
POSTGRES.start();
}
@AfterAll
static void stopAll() {
POSTGRES.stop();
}
}
```
Enable reuse with environment variable: `TESTCONTAINERS_REUSE_ENABLE=true`
## MySQL Container
```java
@Container
static MySQLContainer<?> mysql = new MySQLContainer<>(
DockerImageName.parse("mysql:8.0"))
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");
```
## MongoDB Container
```java
@Container
static MongoDBContainer<?> mongodb = new MongoDBContainer<>(
DockerImageName.parse("mongo:6.0"))
.withExposedPorts(27017);
```
## Kafka Container
```java
@Container
static KafkaContainer kafka = new KafkaContainer(
DockerImageName.parse("confluentinc/cp-kafka:7.5.0"));
@DynamicPropertySource
static void kafkaProperties(DynamicPropertyRegistry registry) {
registry.add("spring.kafka.bootstrap-servers", kafka::getBootstrapServers);
}
```
## Container Initialization
```java
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
"postgres:16-alpine")
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test")
.withInitScript("sql/init-test.sql") // Run init script
.withCommand("postgres", "-c", "max_connections=200"); // Custom config
```
## Network Configuration
```java
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
"postgres:16-alpine")
.withNetwork(Network.SHARED)
.withNetworkAliases("pgdb"); // Access via hostname
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 321k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 321k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 321k | Updated | Claude CodeCodex | Verified | Install |
| 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: 197k | Updated | Claude CodeCodex | Verified | Install |