• 【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());
        }
    }
  • 相关阅读:
    【Android UI设计与开发】第17期:滑动菜单栏(二)开源项目SlidingMenu的示例
    [Usaco2008 Dec]Patting Heads
    Hibernate(六)——多对多关联映射
    2017第2周一小程序
    2017第一周日
    2017第一周六
    无论你如何努力,总会有人讨厌你
    word使用技巧-批量删除图片技巧
    Web跨域问题总结
    JAVA语言的本质优势
  • 原文地址:https://www.cnblogs.com/wangkongming/p/12505699.html
Copyright © 2020-2023  润新知