zoukankan      html  css  js  c++  java
  • 第一个SpringBoot测试实例

    1.SpringBoot项目构建:http://start-spring.io   自动化构建SpringBoot项目,保存在本地并解压

    2.安装gradle并配置gradle环境

    3.配置阿里云maven仓库:修改gradle.build 里面额maven仓库,改为:maven{  url 'http://maven.aliyun.com/nexus/content/groups/public'  }

    4.构建项目,进入解压后的项目路径下,打开控制台执行以下命令:cmd -> cd E:/hello ->E:  ->  gradle build  ->java -jar build/libs/xxxxx.jar

    5.导入开发工具,进行测试,测试代码如下:

    在main目录下,新建HelloController类:

    package com.waylau.spring.boot.blog.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        @RequestMapping("/hello")
        public String hello(){
            return "Hello World!";
        }
    }

    在test下,新建HelloControllerTest类:

    package com.waylau.spring.boot.blog.contrller;
    
    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.hamcrest.Matchers.equalTo;
    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("/hello").accept(MediaType.APPLICATION_JSON))
                    .andExpect(status().isOk())
                    .andExpect(content().string(equalTo("Hello World!")));
        }
    
    }
  • 相关阅读:
    “阿基里斯与乌龟”的终结性思考
    这个世界本来的样子
    Seven times have I despised my soul 《我曾七次鄙视自己的灵魂》
    Youth is not a time of life, it is a state of mind.
    对于过去所犯的错误,最好的道歉是在将来做正确的事
    使用UltraISO刻录自己的音乐CD步骤
    为什么一个目录里放超过十个Mp4文件会导致资源管理器和播放程序变卡变慢?
    用Perl发送邮件小例子
    用df命令显示磁盘使用量和占用率。
    三个JS函数闭包(closure)例子
  • 原文地址:https://www.cnblogs.com/zk-blog/p/10344082.html
Copyright © 2011-2022 走看看