How do you handle JSON responses in Spring Boot tests?
Assert JSON in Spring Boot tests with MockMvc and jsonPath, compare whole documents leniently or strictly, and use MockMvcTester and @JsonTest.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Assert JSON in Spring Boot tests with MockMvc and jsonPath, compare whole documents leniently or strictly, and use MockMvcTester and @JsonTest.
MockMvcBuilders builds MockMvc from a web application context or from standalone controllers. Learn when to use each and how it relates to @WebMvcTest.
Learn to test Spring Boot REST controllers with MockMvc and @WebMvcTest, including JSON assertions, validation errors, security and the AssertJ-based MockMvcTester.
Start the app on a random port with @SpringBootTest and call it through TestRestTemplate, asserting on real status codes, bodies and headers.
@ContextConfiguration tells the Spring TestContext Framework how to build the application context for a test. See classes, locations, caching and Boot alternatives.
Control the Spring Boot test context with @SpringBootTest, profiles, @MockitoBean and @TestConfiguration, and speed tests up with slice annotations and context caching.
Mockito's when() defines what a mock returns or throws when a method is called. See thenReturn, thenThrow, matchers and when to use doReturn instead.
Use when(mock.method()).thenReturn(value) to control what a Mockito mock returns, and doReturn for void methods and spies. Examples use JUnit 5.
@InjectMocks builds the object under test and injects your mocks into it. See the injection order, JUnit 5 setup and typical pitfalls.
Use verify() with times, never or atLeast to check calls on a mock, and ArgumentCaptor to inspect arguments in JUnit 5 tests.
@Mock creates a Mockito mock as a field so you can stub and verify collaborators. Learn setup with MockitoExtension, options and pitfalls.
Create mocks with @Mock, stub them with when and thenReturn, and verify interactions with Mockito 5 and JUnit 5, including captors, spies and strict stubs.
Run one JUnit 5 test with many inputs using @ParameterizedTest. Examples of @ValueSource, @CsvSource, @MethodSource and @EnumSource.
@Disabled skips a JUnit 5 test method or class and reports it as skipped. Learn usage, conditional alternatives and good practice.
Learn the core JUnit 5 assertion methods, how to check exceptions and group assertions, and how AssertJ fits in, with runnable test examples.
@SpringBootTest starts your real Spring context for integration tests. Learn web environments, property overrides, mocking and when to use test slices.
Add spring-boot-starter-test, write JUnit 5 unit tests with Mockito, use test slices for controllers and repositories, and run them with Maven or Gradle.
See how Pageable gives Spring Data JPA bounded, sorted queries, and how to avoid pitfalls like in-memory paging with collection fetches.
Paginate JPA queries with setFirstResult, setMaxResults and a count query, or let Spring Data do it, and avoid fetch join traps.
Learn how to write custom JPA queries with JPQL, named queries, native SQL and the Criteria API, using typed results and bound parameters.