• SpringBoot 单元测试


    一、引入依赖

    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>

    二、测试代码

    package com.example;
    
    import com.example.pojo.Book;
    import com.example.service.BookService;
    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.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;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @RunWith(SpringRunner.class)//用SpringRunner执行测试用例
    @SpringBootTest//这是一个测试类
    public class BookControllerTest {
    
        @Resource
        private BookService bookService;
    
        @Autowired
        private WebApplicationContext wac;
        private MockMvc mockMvc;
        @Before//伪造一个测试环境,不用启动tomcat,会在每次执行测试用例前执行
        public void setup(){
            mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        }
    
        @Test
        public void findAllTest() throws Exception{
    //         List<Book> list = bookService.getAllBooks();
    //        System.out.println("=====findAllTest======="+list.toString());
    //        for(int i=0;i<list.size();i++){
    //            System.out.println(i+"------"+list.get(i).getName());
    //        }
            //执行/findAll请求
            mockMvc.perform(MockMvcRequestBuilders.get("/findAll")
                    //使用UTF8编码
                    .contentType(MediaType.APPLICATION_JSON_UTF8))
                    //希望返回状态码是 isOk() 即:200
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    //希望返回的长度是7
                    .andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(7));
    
        }
    
    
    }
    MockMvcResultMatchers.jsonPath  表达式

    https://github.com/

    https://github.com/json-path/JsonPath

  • 相关阅读:
    C++入门经典-例5.19-指针的引用与传递参数
    C++入门经典-例5.18-通过引用交换数值
    C++入门经典-例5.17-右值引用的定义
    C++入门经典-例5.16-输出引用
    C++入门经典-例5.15-回收动态内存的一般处理步骤
    C++入门经典-例5.14-丢失的内存,关于内存泄漏
    C++入门经典-例5.13-内存安全,被销毁的内存
    C++入门经典-例5.12-动态内存的销毁
    C++入门经典-例5.11-动态分配空间,堆与栈
    C++入门经典-例5.10-指针作为返回值
  • 原文地址:https://www.cnblogs.com/mingforyou/p/14648332.html
Copyright © 2020-2023  润新知