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!")));
        }
    
    }
  • 相关阅读:
    ADB——模拟手机按键输入
    ADB——连接手机的三种方式
    ADB——命令大全
    react-native 入门教程
    react-native-vector-icons 安装
    nginx静态资源缓存与压缩
    ReactNative开发工具有这一篇足矣
    Centos7源代码安装freeswitch和启动freeswitch
    windows 64位下,React-Native环境搭建详解 (Android)
    网站启用SSL后重启Nginx提示 Enter PEM Pass Phrase:需要输入密码
  • 原文地址:https://www.cnblogs.com/zk-blog/p/10344082.html
Copyright © 2011-2022 走看看