码迷,mamicode.com
首页 > 编程语言 > 详细

第一个SpringBoot测试实例

时间:2019-01-31 22:50:42      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:项目   仓库   str   text   cmd   frame   ott   hello   class   

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!")));
    }

}

 

第一个SpringBoot测试实例

标签:项目   仓库   str   text   cmd   frame   ott   hello   class   

原文地址:https://www.cnblogs.com/zk-blog/p/10344082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!