脑子——认识的能力——是像肌肉一样,靠练习、锻炼而培养起来的。——高尔基 今天遇到个问题: 1Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.test.web.client.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. 原来是SpringBootTest没加webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT 加上就好了 123456789101112131415161718192021222324import jakarta.annotation.Resource;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.boot.test.web.client.TestRestTemplate;/** * XxxControllerTest * * @author achao@apache.org */@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // 确保使用RANDOM_PORTpublic class XxxControllerTest { @Resource private TestRestTemplate restTemplate; @Test void generateTokenTest() { var res = restTemplate.getForEntity("https://xxx.xx", Res.class).getBody(); Assertions.assertNotNull(res); }}