Index of every page on this site: llms.txt
All skills
github avatar

/spring-boot-testing

@f10fdb8 official
by githubgithub/awesome-copilot42k stars
5,426
Run it yourself

Expert Spring Boot testing specialist that selects the best testing techniques for your situation: test slices, Testcontainers with @ServiceConnection, Failsafe integration tests, MockitoBean, JUnit and AssertJ. Written for Spring Boot 4 with notes for Boot 3.

Use this Skill: https://skilld.dev/gh/github/awesome-copilot/spring-boot-testing

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencestestcontainers-jdbc.md

≈2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Testcontainers JDBC

Testing JPA repositories with real databases using Testcontainers.

Overview

Testcontainers provides real database instances in Docker containers for integration testing. More reliable than H2 for production parity.

Quickstart: @ServiceConnection (PostgreSQL)

Check the project's versions first. Testcontainers 2.x (Spring Boot 4) and 1.x (Spring Boot 3) differ in artifact and package names:

Testcontainers 2.x (Boot 4) Testcontainers 1.x (Boot 3.1+)
PostgreSQL artifact testcontainers-postgresql postgresql
JUnit 5 integration testcontainers-junit-jupiter junit-jupiter
Container class org.testcontainers.postgresql.PostgreSQLContainer org.testcontainers.containers.PostgreSQLContainer

@ServiceConnection (Spring Boot 3.1+) replaces @DynamicPropertySource boilerplate. Versions of these artifacts are managed by the Spring Boot BOM.

Dependencies (Boot 4 / Testcontainers 2.x)

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-testcontainers</artifactId>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.testcontainers</groupId>
  <artifactId>testcontainers-postgresql</artifactId>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.testcontainers</groupId>
  <artifactId>testcontainers-junit-jupiter</artifactId>
  <scope>test</scope>
</dependency>

Option A: shared configuration (recommended, reusable across tests)

@TestConfiguration(proxyBeanMethods = false)
public class TestcontainersConfiguration { // public: tests in sub-packages import it

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

@SpringBootTest
@Import(TestcontainersConfiguration.class)
class OrderIT { }

Starting the app against the same container for local development: SpringApplication.from(Application::main).with(TestcontainersConfiguration.class).run(args).

Option B: per-class container

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
class OrderRepositoryPostgresTest {
  
  @Container
  @ServiceConnection
  static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:18");
  
  @Autowired
  private OrderRepository orderRepository;
  
  @Autowired
  private TestEntityManager entityManager;
}

Note: Order is a reserved word in SQL; an entity named Order needs @Table(name = "orders") or PostgreSQL fails with syntax error at or near "order" (verified).

MySQL Setup

<dependency>
  <groupId>org.testcontainers</groupId>
  <artifactId>testcontainers-mysql</artifactId>
  <scope>test</scope>
</dependency>
import org.testcontainers.mysql.MySQLContainer; // 2.x

@Container
@ServiceConnection
static MySQLContainer mysql = new MySQLContainer("mysql:8.4");

Testcontainers 1.x (Boot 3) variant: org.testcontainers.containers.MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.4") with the mysql artifact.

Multiple Databases

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
class MultiDatabaseTest {
  
  @Container
  @ServiceConnection(name = "primary")
  static PostgreSQLContainer primaryDb = new PostgreSQLContainer("postgres:18");
  
  @Container
  @ServiceConnection(name = "analytics")
  static PostgreSQLContainer analyticsDb = new PostgreSQLContainer("postgres:18");
}

Container Reuse (Speed Optimization)

Cross-run reuse is local-dev only. Opt in with testcontainers.reuse.enable=true in ~/.testcontainers.properties. @Container does not give cross-run reuse because the extension stops the container after the tests. Start the container manually, without extension-managed shutdown:

@ServiceConnection
static final PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:18")
  .withReuse(true);

static {
  postgres.start();
}

For the generic lifecycle, singleton and reuse rules, use the java-testcontainers skill.

Database Initialization

With SQL Scripts

@Container
@ServiceConnection
static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:18")
  .withInitScript("schema.sql");

With Flyway

@SpringBootTest
@Testcontainers
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class MigrationTest {
  
  @Container
  @ServiceConnection
  static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:18");
  
  @Autowired
  private Flyway flyway;
  
  @Test
  void shouldApplyMigrations() {
    flyway.migrate();
    // Test code
  }
}

Advanced Configuration

Custom Database/Schema

@Container
@ServiceConnection
static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:18")
  .withDatabaseName("testdb")
  .withUsername("testuser")
  .withPassword("testpass")
  .withInitScript("init-schema.sql");

Wait Strategies

@Container
static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:18")
  .waitingFor(Wait.forLogMessage(".*database system is ready.*", 1));

Test Example

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
class OrderRepositoryTest {
  
  @Container
  @ServiceConnection
  static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:18");
  
  @Autowired
  private OrderRepository orderRepository;
  
  @Autowired
  private TestEntityManager entityManager;
  
  @Test
  void shouldFindOrdersByStatus() {
    // Given
    entityManager.persist(new Order("PENDING"));
    entityManager.persist(new Order("COMPLETED"));
    entityManager.flush();
    
    // When
    List<Order> pending = orderRepository.findByStatus("PENDING");
    
    // Then
    assertThat(pending).hasSize(1);
    assertThat(pending.get(0).getStatus()).isEqualTo("PENDING");
  }
  
  @Test
  void shouldSupportPostgresSpecificFeatures() {
    // Can use Postgres-specific features like:
    // - JSONB columns
    // - Array types
    // - Full-text search
  }
}

@DynamicPropertySource Alternative

If not using @ServiceConnection:

@SpringBootTest
@Testcontainers
class OrderServiceTest {
  
  @Container
  static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:18");
  
  @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);
  }
}

Supported Databases

Container classes (2.x) live in org.testcontainers.<module> and are non-generic (e.g. org.testcontainers.mysql.MySQLContainer).

Database Container Class Maven Artifact
PostgreSQL PostgreSQLContainer testcontainers-postgresql
MySQL MySQLContainer testcontainers-mysql
MariaDB MariaDBContainer testcontainers-mariadb
SQL Server MSSQLServerContainer testcontainers-mssqlserver
Oracle OracleContainer testcontainers-oracle-free
MongoDB MongoDBContainer testcontainers-mongodb

Running *IT classes

Integration tests named *IT run in Maven's Failsafe, not Surefire; see failsafe-integration-tests.md or they will silently not run.

Best Practices

  1. Use @ServiceConnection when possible (Spring Boot 3.1+)
  2. Enable container reuse for faster local builds
  3. Use specific versions (postgres:18) not latest
  4. Keep container config in static field
  5. Use @DataJpaTest with AutoConfigureTestDatabase.Replace.NONE

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts18d4 checks · Risk SAFE
  • Gen Agent Trust Hub18d

    This skill is a comprehensive educational resource for Spring Boot testing. It provides best practices and code templates for modern Java testing frameworks. No malicious patterns, vulnerabilities, or security risks were identified during the analysis.

  • Socket18d

    No alerts

  • Snyk18d

    Risk: LOW · No issues

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at f10fdb8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated last month
  • Testing
  • spring-boot
  • junit
  • assertj
  • mockmvc
  • testcontainers
  • java
  • mockito
  • integration-testing
  • webmvctest

README badge

README badge for github/awesome-copilot/spring-boot-testing

Guides Spring Boot 4 testing with test slices (@WebMvcTest, @DataJpaTest, @RestClientTest), modern assertion libraries (AssertJ, MockMvcTester), and practical patterns like Testcontainers for database integration and context caching for performance. Covers the test pyramid, complexity assessment, and 80% coverage goals.

Generated from the current SKILL.md.

Does this skill work with Spring Boot 3, or is it Spring Boot 4 only?
This skill is written for Spring Boot 4. It covers Spring Boot 4-specific features like RestTestClient, @MockitoBean, and MockMvcTester. Some patterns (like traditional MockMvc) have backward-compatible alternatives documented, but the primary guidance assumes Spring Boot 4.
What assertion library does this skill recommend?
AssertJ. The skill explicitly prefers AssertJ's fluent, readable assertion style over verbose matchers, and includes references for assertions on scalars, collections, and other data types.
Does this cover testing with real databases, or only mocks?
Both. The skill recommends Testcontainers for data layer tests (@DataJpaTest) to use real databases in tests, and includes references for PostgreSQL, MySQL, and other JDBC sources.
What's the recommended test coverage target?
80% as a practical balance between quality and effort. The skill prioritizes business-critical paths, complex algorithms, error handling, and integration points over achieving maximum coverage.
Does this skill help with test data generation?
Yes. It references Instancio for generating complex test objects with 3+ properties, reducing boilerplate in test setup.

Generated from the current SKILL.md. These answers refresh after source changes.