- 前言
- 配置web环境
- 发起虚拟调用
Springboot的测试中配置web环境和虚拟调用
配置web环境使用注解@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT) 配置web环境
@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT) 配置说明:
- SpringBootTest.WebEnvironment.DEFINED_PORT
固定端口,与application.yml配置一致 - SpringBootTest.WebEnvironment.NONE
(默认状态)不是web环境 - SpringBootTest.WebEnvironment.RANDOM_PORT
随机端口
虚拟请求调用,调用接口 http://localhost:xxxx/hello
@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc //开启mvc调用
public class WebTest {
//通过形参方式注入Mvc对象
@Test
void hello(@Autowired MockMvc mockMvc) throws Exception {
//创建请求,http://localhost:xxxx/hello
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/hello");
ResultActions action=mockMvc.perform(builder);
}
}
其他的请求方式



