zoukankan      html  css  js  c++  java
  • MockMvc 对 Spring Boot 进行单元测试

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.http.MediaType;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    public class HelloControllerTest {
    
        @Autowired
        private MockMvc mockMvc;
    
        @Test
        public void testHello() throws Exception {
            
            mockMvc.perform(MockMvcRequestBuilders.get("/hi").accept(MediaType.APPLICATION_JSON))
                    .andExpect(status().isOk())
                    .andExpect(content().string("Hello world"));
        }
    }
    
    

  • 相关阅读:
    sql注入常用的判断方法
    refere是什么
    mysql语句
    linux命令
    软件测试
    tcp和udp
    java面试干货
    为什么重写了equals方法一般也要重写hashCode方法
    redis源码解析
    Numpy复习
  • 原文地址:https://www.cnblogs.com/bqh10086/p/MockMvc-Spring-Boot.html
Copyright © 2011-2022 走看看