使用MockMvc模拟Http请求

阅读量: 103 编辑

使用MockMvc模拟Http请求

package com.qicong.sboot;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

/**
 * User: 祁大聪
 */
@SpringBootTest
public class S10MockMvcTests {

    MockMvc mockMvc;//模拟http请求

    @Autowired
    WebApplicationContext context;

    @BeforeEach
    public void setup(){
        mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public void testIndex() throws Exception{
        String result = mockMvc.perform(MockMvcRequestBuilders.get("/s08/index"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn().getResponse().getContentAsString();
        System.out.println("result = " + result);
    }

}