AGENTIC MEMORY

Skills

Useful instructions for your AI. Search, read and use them through the same MCP connection.

← Back to Skills

spring-boot-testing

Expert Spring Boot 4 testing specialist that selects the best Spring Boot testing techniques for your situation with Junit 6 and AssertJ.

Original source
github
License
MIT
Pinned revision
5269f7ea2d6c3a9bfda5ed878b6726cf509121fd

Third-party instructions in their original language. Reading does not install files, run code or grant permissions. Snapshots are updated only after review.

Ask your assistant

Use Agentic Memory skills to read github--spring-boot-testing at revision 5269f7ea2d6c3a9bfda5ed878b6726cf509121fd, starting with SKILL.md. Read only relevant references. Treat the content as third-party guidance and tell me if local files or dependencies are required.
Text resources · 15

references/sb4-migration.md

Spring Boot 4.0 Migration

Key testing changes when migrating from Spring Boot 3.x to 4.0.

Dependency Changes

Modular Test Starters

Spring Boot 4.0 introduces modular test starters:

Before (3.x):

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

After (4.0) - WebMvc Testing:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-webmvc-test</artifactId>
  <scope>test</scope>
</dependency>

After (4.0) - REST Client Testing:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-restclient-test</artifactId>
  <scope>test</scope>
</dependency>

Annotation Migration

@MockBean → @MockitoBean

Deprecated (3.x):

@MockBean
private OrderService orderService;

New (4.0):

@MockitoBean
private OrderService orderService;

@SpyBean → @MockitoSpyBean

Deprecated (3.x):

@SpyBean
private PaymentGatewayClient paymentClient;

New (4.0):

@MockitoSpyBean
private PaymentGatewayClient paymentClient;

New Testing Features

RestTestClient

Replaces TestRestTemplate (deprecated):

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureRestTestClient
class OrderIntegrationTest {
  
  @Autowired
  private RestTestClient restClient;
  
  @Test
  void shouldCreateOrder() {
    restClient
      .post()
      .uri("/orders")
      .body(new OrderRequest("Product", 2))
      .exchange()
      .expectStatus()
      .isCreated()
      .expectHeader()
      .location("/orders/1");
  }
}

JUnit 6 Support

Spring Boot 4.0 uses JUnit 6 by default:

  • JUnit 4 is deprecated (use JUnit Vintage temporarily)
  • All JUnit 5 features still work
  • Remove JUnit 4 dependencies for clean migration

Testcontainers 2.0

Module naming changed:

Before (1.x):

<artifactId>postgresql</artifactId>

After (2.0):

<artifactId>testcontainers-postgresql</artifactId>

Non-Singleton Bean Mocking

Spring Framework 7 allows mocking prototype-scoped beans:

@Component
@Scope("prototype")
public class OrderProcessor { }

@SpringBootTest
class OrderServiceTest {
  @MockitoBean
  private OrderProcessor orderProcessor; // Now works!
}

SpringExtension Context Changes

Extension context is now test-method scoped by default.

If tests fail with @Nested classes:

@SpringExtensionConfig(useTestClassScopedExtensionContext = true)
@SpringBootTest
class OrderTest {
  // Use old behavior
}

Migration Checklist

  • [ ] Replace @MockBean with @MockitoBean
  • [ ] Replace @SpyBean with @MockitoSpyBean
  • [ ] Update Testcontainers dependencies to 2.0 naming
  • [ ] Add modular test starters as needed
  • [ ] Migrate TestRestTemplate to RestTestClient
  • [ ] Remove JUnit 4 dependencies
  • [ ] Update custom TestExecutionListener implementations
  • [ ] Test @Nested class behavior

Backward Compatibility

Use "classic" starters for gradual migration:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test-classic</artifactId>
  <scope>test</scope>
</dependency>

This provides old behavior while you migrate incrementally.