The following test case works well on spring boot 3.5:
package br.gov.ce.seplag.adbens.controllers;
import br.gov.ce.seplag.adbens.models.organizacao.Orgao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.test.client.TestRestTemplate;
import org.springframework.test.context.ActiveProfiles;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OrgaosControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
void deveriaListarOrgaos() {
var result = restTemplate.getForObject("/orgaos", Orgao[].class);
assertThat(result, notNullValue());
}
}
In spring boot 4 it fails on autowiring the TestRestTemplate
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.web.server.test.client.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Comment From: bclozel
I think the RestClient support is not brought in transitively anymore by the test starter. Can you add "spring-boot-starter-restclient" as a test dependency and report back?