• 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("中文测试"));//
        }
    }
  • 相关阅读:
    Jupsh_flutter Android 收不到消息 / 排查不出请根据第 9 点说明提供信息
    angular表单 Dom获取表单值以及双向数据绑定
    angular中的组件以及组件中的模板合成
    Flutter开发的app进行设备判断是Ios还是android
    Java的封装
    GCD编程 之 略微提高篇
    多线程基础(六)GCD基础
    我遇到的CocoaPods的问题(也许后期会解决,持续更新)
    iOS之通过PaintCode快速实现交互动画的最方便方法 未解问题
    多线程基础(五)NSThread线程通信
  • 原文地址:https://www.cnblogs.com/jpit/p/7503656.html
Copyright © 2020-2023  润新知