• spring boot单元测试之MockMvc


    spring单元测试之MockMvc,这个只是模拟,并不是真正的servlet,所以session、servletContext是没法用的。

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @WebAppConfiguration
    public class UrlTests {
        @Autowired
        private WebApplicationContext webContext;
    
        private MockMvc mockMvc;
    
        @Before
        public void setupMockMvc() throws Exception {
            mockMvc = MockMvcBuilders.webAppContextSetup(webContext).build();
        }
    
        @Test
        public void testGet() throws Exception {
            System.err.println("========================");
            Cookie cookies = new Cookie("cookie", "cook");
            mockMvc.perform(get("/index?name=xiaoming")
                    .header("header", "hehe")
                    .cookie(cookies)
                    .requestAttr("name", "pangbin")
                    .sessionAttr("name", "panggao")
                    .characterEncoding("UTF-8"))
                    .andExpect(status().isOk())
                    .andDo(MockMvcResultHandlers.print(System.err))
                    .andReturn();
            System.err.println("========================");
        }
    
        @Test
        public void testPost() throws Exception {
            System.err.println("========================");
            mockMvc.perform(post("/index")
                    .header("header", "hehe")
                    .param("body", "baby!"))
                    .andExpect(status().isOk())
                    .andDo(MockMvcResultHandlers.print(System.err))
                    .andReturn();
            System.err.println("========================");
        }
    }
  • 相关阅读:
    TabControl
    Loading
    Dialog
    Combobox
    Markdown编辑器Editor.md使用方式
    XSS攻击
    跨域解决方案及实现
    angular4 自定义表单组件
    angular4 Form表单相关
    js 详解setTimeout定时器
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/9182981.html
Copyright © 2020-2023  润新知