Testing
Testing Spring Security
This article explains how to test authentication and authorization in Spring Boot applications, covering both MVC and reactive WebFlux. It shows how to secure endpoints, simulate different user roles, handle CSRF protection, and verify access for authorized and unauthorized users. It also covers strategies for unit, integration, and end-to-end testing, along with tips for troubleshooting security issues.
Introduction to Testing Spring Boot Applications
This article provides an introduction to testing Spring Boot applications. It covers the basics of unit testing with JUnit and Mockito, testing slices of the application, and full integration tests. It offers a starting point for writing effective, maintainable tests and understanding the tools and strategies available in Spring Boot.
Spring Boot Testing Strategy
This article outlines a practical Spring Boot testing strategy, focusing on writing fast, reliable, and expressive tests. It covers using mocks wisely across architectural boundaries, writing narrow integration tests for external interactions, simulating dependencies locally, and keeping code maintainable through clear separation of logic and infrastructure. The approach balances unit, integration, and broader tests for effective coverage.
Spring Boot Integration Testing With @SpringBootTest
This article explores integration testing in Spring Boot, including testing in mock environments, running a real server for end-to-end tests, simulating external dependencies, managing test data, and configuring the test setup for speed and reliability.
Testing Spring Boot WebClient With MockWebServer
This article explains why mocking WebClient directly can be brittle and complex. It demonstrates how to write integration tests using a mock server to verify requests, responses, and error handling. The approach ensures reliable testing of request serialization, response deserialization, and behavior under errors.
Testing Serialization With Spring Boot @JsonTest
This article explains why testing JSON serialization and deserialization separately can improve reliability, especially for custom types or formats. It shows how to use lightweight test setups to verify that objects are correctly converted to and from JSON. Examples cover both serialization and deserialization with practical assertions for expected output and input.
Testing the Persistence Layer With Spring Boot @DataJpaTest
This article explains how to write reliable tests for the persistence layer. It covers creating database schemas, managing test data, and testing queries, constraints, and edge cases. Using the same database for tests and application ensures migrations and native queries behave as expected.
Testing Web Controllers With Spring Boot @WebMvcTest
This article explains why unit tests alone are not enough to verify Spring Boot controllers. It demonstrates how to use integration tests to check HTTP request mapping, JSON serialization and deserialization, input validation, exception handling, and interaction with business logic. The approach ensures controllers behave correctly in realistic web scenarios.
Spring Boot Unit Testing
This article explains how to write effective unit tests in Spring Boot by testing components in isolation without loading the full application context. It covers why field injection slows tests, how constructor injection makes services testable, and how to use Mockito for fast, reliable tests. The approach ensures unit tests run quickly and focus solely on the behavior of the code under test.
JUnit 5 Nested Tests: Grouping Related Tests Together
This tutorial demonstrates how to use JUnit 5 nested tests to structure tests hierarchically, making it easier to group related cases and navigate results. It covers simple examples with nested classes, multi-layered nesting for REST controllers, and tips to avoid pitfalls like excessive duplication. Using nested tests improves readability and helps maintain clean, organized test code.