How do you use Spring's TestRestTemplate for integration testing?
Start the app on a random port with @SpringBootTest and call it through TestRestTemplate, asserting on real status codes, bodies and headers.
2,881 posts about Java: questions with answers, tutorials and articles, newest first.
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.
The EntityManager interface is JPA's entry point for entity persistence and queries. Learn its methods, how to obtain it and common pitfalls.
Add spring-boot-starter-data-jpa, configure a datasource, map entities with jakarta.persistence and extend JpaRepository to get CRUD without writing SQL.
IContext is best known from Thymeleaf, where it supplies variables and a locale to templates. Learn its methods, implementations and design tips.