@Slf4j
@TestConfiguration// 开启通过@Bean替换原有Bean
@ActiveProfiles("dev")// 指定环境
@TestPropertySource(properties = {
"logging.pattern.console=%d{YYYY-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n",
"logging.level.root=INFO"
})// 修改配置
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)// 随机端口避免冲突
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) // 控制测试顺序(可选)
class ProxyApplicationTests {
@Autowired
private TestRestTemplate restTemplate; // 用于发起 HTTP 请求
@BeforeEach
void setUp() {
// 每个测试前执行
}
@Test
@Order(1)
@DisplayName("用户名获取用户列表流程")
void contextLoads() {
log.info("测试开始");
// 第一步 GET 请求
ResponseEntity<User> response = restTemplate.exchange(
"/test?name=张三",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<User>() {}
);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);// 断言 HTTP 状态码
assertThat(response.getBody()).isNotEqualTo(null);
assertThat(response.getBody().getName()).isEqualTo("张三");
User user = response.getBody();
log.info("用户: {}", user);
// 第二步 POST 请求
HttpEntity<User> request2 = new HttpEntity<>(user);
ResponseEntity<List<User>> response2 = restTemplate.exchange(
"/test2",
HttpMethod.POST,
request2,
new ParameterizedTypeReference<List<User>>() {}
);
assertThat(response2.getStatusCode()).isEqualTo(HttpStatus.OK);// 断言 HTTP 状态码
assertThat(response2.getBody()).asList().isNotEmpty();
final List<User> body = response2.getBody();
log.info( "用户列表: {}", body);
log.info("测试结束");
}
}