@SpringBootTest

`@SpringBootTest` loads a Spring Boot application context so a test can exercise multiple application components together. It is useful when a test needs more of the real application configuration than a focused unit or slice test provides.

Place `@SpringBootTest` on a test class when the test needs Spring Boot to create an application context. The context is built using the application's configuration, allowing components such as services, repositories, controllers, configuration, and dependency injection to work together.

By default, this annotation does not mean that a real HTTP server must start. For web applications, the test can use different web environments, including a mock web environment or an actual server, depending on the `webEnvironment` setting.

Spring can reuse an application context between compatible tests, which can reduce startup cost. However, tests that modify the context configuration may prevent reuse and cause additional context initialization.

`@SpringBootTest` is broader than focused testing annotations such as `@WebMvcTest` or `@DataJpaTest`. That broader scope makes it appropriate for integration-style tests, but it can also make tests slower and more dependent on application configuration.

Key points

Interview questions

What does `@SpringBootTest` do?

`@SpringBootTest` tells Spring Boot to create an application context for the test. This allows the test to use Spring dependency injection and multiple real application components together. It is commonly used for integration-style testing rather than isolated unit testing.

Does `@SpringBootTest` start an HTTP server?

Not necessarily. The web environment is controlled by the annotation's `webEnvironment` configuration. A mock web environment can be used without binding a real server port, while an appropriate server-based setting can start an actual embedded server.

Why might you choose `@SpringBootTest` instead of `@WebMvcTest`?

`@SpringBootTest` is appropriate when the test needs components beyond the MVC layer, such as services, repositories, and application configuration. `@WebMvcTest` deliberately narrows the loaded context to web-related components. The broader annotation therefore provides more realistic application integration but generally costs more startup time.

How can `@SpringBootTest` tests become faster?

First, use narrower test types when the test does not require the complete application context. When full-context tests are necessary, compatible tests can benefit from Spring's context caching. Avoid unnecessary context customizations that force additional contexts to be created.

How would you test the application context itself with `@SpringBootTest`?

A minimal test can annotate the test class with `@SpringBootTest` and provide an empty `@Test` method. If Spring cannot create the configured application context, initialization fails and the test fails. This can catch configuration or bean wiring problems that isolated unit tests may not detect.