zoukankan      html  css  js  c++  java
  • Spring Boot Test

    package com.example.demo;
    
    import static org.hamcrest.Matchers.containsString;
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.http.MediaType;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.context.web.WebAppConfiguration;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
    import org.springframework.test.web.servlet.setup.MockMvcBuilders;
    import org.springframework.web.context.WebApplicationContext;
    
    import com.google.gson.GsonBuilder;
    
    @WebAppConfiguration
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = {DemoApplication.class})
    public class ApplicationMockBase {
    
        @Autowired
        private WebApplicationContext context;
    
        private MockMvc mockMvc;
    
        @Before
        public void before() {
            mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
        }
    
    
        @After
        public void after() {}
    
        public String data() {
            RequestDto dto = new RequestDto();
            dto.setKeyword("这是一个关键字");
            dto.setToken("test");
            return new GsonBuilder().create().toJson(dto);
        }
    
        @Test
        public void test() throws Exception {
            mockMvc.perform(post("/portal/post").//
                    contentType(MediaType.APPLICATION_JSON_UTF8).//
                    content(data()).//
                    accept(MediaType.APPLICATION_JSON_UTF8)).//
                    andDo(MockMvcResultHandlers.print()).//
                    andExpect(status().isOk()).//
                    andExpect(content().string(containsString("succ"))).//
                    andExpect(jsonPath("$.code").value("0")).//
                    andExpect(jsonPath("$.name").value("中文测试"));//
        }
    }
  • 相关阅读:
    招聘.Net中高级软件研发工程师
    布局和救火
    UITableView详解(转)
    iOS开发那些事--性能优化–内存泄露问题的解决(转)
    LeeCode(PHP) 2.add-two-numbers
    LeeCode(PHP) 1.Two Sum
    PHP实现 序列帧拆分
    PHPExcel导出大量数据超时及内存错误解决方法(转)
    laravel路由 实现短连接生成及跳转(php 301重定向)
    从扑克牌中随机抽取5张牌,判断是不是一个顺子,即这5张牌是不是连续(面试题)
  • 原文地址:https://www.cnblogs.com/jpit/p/7503656.html
Copyright © 2011-2022 走看看