zoukankan      html  css  js  c++  java
  • 【spring-boot】写一个简单的单元测试

    application.yml

    server:
      port: 8090
      servlet:
        context-path: /springboot

    HelloController

    package com.komiles.study.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/hello")
    public class HelloController {
    
        @GetMapping("/test")
        public String test(@RequestParam(value = "name", defaultValue = "1234")  String name)
        {
            return name;
        }
    }

    HelloControllerTest

    package com.komiles.study.controller;
    
    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.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;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    public class HelloControllerTest {
    
        @Autowired
        private MockMvc mockMvc;
    
        @Test
        public void test() throws Exception {
            this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/test"))
                    .andExpect(MockMvcResultMatchers.status().isOk());
        }
    }
  • 相关阅读:
    2. Redis哨兵、复制、集群的设计原理与区别
    1. 详解Redis的存储类型、集群架构、以及应用场景
    博客园主题优化
    【Java基础】Java面试题精选
    【Java集合】——集合类分析总结
    新零售供应链的三大闭环
    Comparable和Comparator比较实现排序 场景分析
    Java基础-枚举类
    Java基础-泛型
    微服务架构~BFF和网关
  • 原文地址:https://www.cnblogs.com/wangkongming/p/12505699.html
Copyright © 2011-2022 走看看