В тестах на Spring Boot можно без лишних настроек использовать
RestTestClient через аннотацию @AutoConfigureRestTestClientДля TestRestTemplate есть аналогичная аннотация —
@AutoConfigureTestRestTemplate@SpringBootTest
@AutoConfigureRestTestClient
public class PersonControllerTests {
private static final String API_PATH = "/persons";
@Test
void add(@Autowired RestTestClient restTestClient) {
restTestClient.post().uri(API_PATH)
.body(Instancio.create(Person.class))
.exchange()
.expectStatus().is2xxSuccessful()
.expectBody(Person.class)
.value(person -> assertNotNull(person.getId()));
}
}
👉 Java Portal
