首页 > 编程语言 > 详细

SpringBoot使用MockMVC单元测试Controller

时间:2021-05-26 21:28:58      阅读:12      评论:0      收藏:0      [点我收藏+]
@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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!