@Slf4j @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc public class HellowordController { private static final String apiHellowordUrl = "/helloword"; private MockMvc mockMvc; @Autowired private WebApplicationContext webApplicationContext; @Before public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void testHelloword() throws Exception { MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(apiHellowordUrl ) .param("p1", "1") .header("Authorization", "871d6a96ec94b4e85fbb5d77931605a") ).andReturn(); log.info(mvcResult.getResponse().getContentAsString()); } }
SpringBoot使用MockMVC单元测试Controller
原文:https://www.cnblogs.com/wanfeng/p/14814673.html