Testing Spring Boot Applications

Testing Spring Boot Applications involves verifying application logic, component wiring, and system interactions across different layers of the software stack to ensure reliability, correctness, and maintainability. Spring Boot simplifies this process through the spring-boot-starter-test dependency, which bundles industry-standard libraries (such as JUnit 5, Mockito, AssertJ, and Hamcrest) and provides specialized annotations for isolated unit testing, layer-specific slice testing, and end-to-end integration testing.

1. Unit Testing

Unit testing focuses on testing individual classes, methods, or components in complete isolation from external systems and the Spring application context.

  • Execution Speed: Does not start the Spring container or database connections, allowing tests to run rapidly in memory.
  • Target: Typically applied to service-layer business logic, utility classes, and custom validators.
  • Framework: Primarily uses JUnit 5 (Jupiter) assertions and test lifecycle annotations (such as @Test, @BeforeEach, and @ParameterizedTest).

2. Mockito

Mockito is a mocking framework used to simulate the behavior of complex external dependencies (such as databases, third-party REST clients, or downstream services) so the unit under test can be verified independently.

  • Core Annotations: Uses @Mock to create simulated instances and @InjectMocks to inject those mocks into the class being tested.
  • Stubbing & Verification: Employs methods like Mockito.when(...).thenReturn(...) to simulate method outcomes and Mockito.verify(...) to confirm that specific methods were invoked with expected arguments.
  • Spring Integration: Spring provides @MockBean (or @MockitoBean in newer Spring releases) to seamlessly replace beans inside a Spring test context with Mockito-managed mocks.

3. Controller Testing

Controller testing verifies the web/presentation layer, ensuring endpoints handle HTTP requests, path variables, query parameters, validation, and JSON serialization correctly.

  • @WebMvcTest: A slice-test annotation that boots only the web infrastructure (controllers, filters, converters) rather than the complete application context.
  • MockMvc: Simulates HTTP requests (such as GET, POST, PUT, DELETE) without spinning up a real network server.
  • Assertions: Validates HTTP response status codes, headers, and response payloads using JSONPath expressions (e.g., jsonPath("$.name").value("Alice")).

4. Repository Testing

Repository testing validates the persistence layer, ensuring that SQL/JPQL queries, schema mappings, derived repository methods, and database constraints function as expected.

  • @DataJpaTest: A specialized test slice annotation that configures only Spring Data JPA repositories, entity classes, and the EntityManager.
  • Database Setup: Typically runs against a lightweight in-memory database (such as H2) or a real containerized database using Testcontainers.
  • Automatic Rollback: By default, @DataJpaTest methods run inside a transaction that rolls back automatically at the end of each test, keeping test data isolated.

5. Integration Testing

Integration testing verifies that multiple layers of the application—from the web tier and service layer down to the database and security configurations—work cohesively together.

  • @SpringBootTest: Loads the entire Spring application context, mirroring production deployment as closely as possible.
  • Server Configurations: Can launch an embedded web server on a random port (webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) to test actual network calls over HTTP.
  • HTTP Clients: Uses TestRestTemplate or WebTestClient to send real HTTP requests to the running test server and inspect full system responses end-to-end.
Scroll to Top