脑子——认识的能力——是像肌肉一样,靠练习、锻炼而培养起来的。——高尔基

今天遇到个问题:

1
Caused 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

加上就好了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import 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_PORT
public class XxxControllerTest {

@Resource
private TestRestTemplate restTemplate;

@Test
void generateTokenTest() {
var res = restTemplate.getForEntity("https://xxx.xx", Res.class).getBody();
Assertions.assertNotNull(res);
}

}