How do you mock external services in Spring Boot REST API tests?
Replace external clients with @MockitoBean, fake HTTP calls with MockRestServiceServer or WireMock, and test error paths without touching real services.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Replace external clients with @MockitoBean, fake HTTP calls with MockRestServiceServer or WireMock, and test error paths without touching real services.
How @SpringBootTest fits into Spring Boot testing, from real database integration tests to mocking boundaries and keeping the context cache effective.
Test Spring Boot REST endpoints end to end with @SpringBootTest, MockMvc or a real HTTP client, and a Testcontainers database that matches production.
Learn how to map bidirectional JPA relationships correctly: owning vs inverse side, mappedBy, sync helpers, cascading and common pitfalls.
FetchType tells JPA when to load data, EAGER at once or LAZY on first access. Learn the defaults per mapping, common errors and per-query fixes.
CascadeType lists the six EntityManager operations that propagate to related entities in JPA: PERSIST, MERGE, REMOVE, REFRESH, DETACH and ALL. See what each does and when to use it.
MockMvc sends simulated HTTP requests to Spring MVC controllers so you can test mappings, validation and JSON fast. See setup and examples.
Use the cascade attribute and orphanRemoval to propagate persist, merge and remove from a parent entity to its children, and know when not to.
Combine @WebMvcTest with MockMvc, @DataJpaTest and @SpringBootTest to test controllers, repositories and full REST flows, using @MockitoBean on current Boot.
@IdClass maps a composite primary key by pairing an entity's @Id fields with a separate Serializable key class. See the rules, examples and how it compares to @EmbeddedId.
CorsConfiguration is the Spring object that describes a CORS policy. Learn its settings and how to apply it globally with Spring MVC and Spring Security.
Map a multi-column primary key in JPA using @EmbeddedId or @IdClass, with runnable Jakarta Persistence examples, @MapsId and Spring Data usage.
Configure CORS in a Spring Boot REST API globally, per controller and with Spring Security 6, and learn how to debug preflight failures.
@Embedded stores an embeddable value object in the owning entity's table. Learn column overrides, queries, null handling and when to use an entity instead.
Design a consistent JSON error contract for a Spring Boot REST API with custom exceptions, @RestControllerAdvice, validation details and ProblemDetail.
Use a @Version field so JPA rejects stale updates with an OptimisticLockException, then retry or return a conflict to the user.
@EntityListeners runs listener classes on JPA lifecycle events such as persist and update. Learn custom listeners and Spring Data JPA auditing.
@ExceptionHandler lets a Spring Boot API return clean, consistent error responses. See ProblemDetail, validation handling and MockMvc tests.
Make repository.delete() update a flag instead of removing the row. Covers Hibernate @SoftDelete, @SQLDelete, restoring rows and pitfalls with batch deletes.
Learn how to centralise exception handling in Spring Boot with @RestControllerAdvice, custom exceptions and ProblemDetail responses that follow RFC 9457.