API文档是前端与后端快速开发,减少沟通成本的必要条件,有一份完善的文档是很必要的,由通过测试来生成文档的好处就是:测试数据有了,测试返回结果有了,而且可以对这些字段进行说明,很清晰,在springboot框架里,去使用mockMvc文档生成时,需要有以下几个步骤,大叔总结了一下,分享给大家。

一 mockMvc包引用

testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
asciidoctor 'org.springframework.restdocs:spring-restdocs-asciidoctor'

二 snippetsDir插件引用

在buildscript块里添加如下代码

maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
dependencies {
classpath "org.asciidoctor:asciidoctor-gradle-jvm:2.0.0-rc.1" }

添加插件

apply plugin: "org.asciidoctor.convert"

三 配置三大路径的地址,三大路径指,asciidoctor文档路径,生成的API文档目录和snippets目录

jar {
dependsOn asciidoctor
from ("${asciidoctor.outputDir}/html5") {
into 'static/docs'
}
} ext {
snippetsDir = file('build/generated-snippets')
} integTest {
outputs.dir snippetsDir
} asciidoctor {
inputs.dir snippetsDir
outputDir "build/asciidoc"
dependsOn integTest
sourceDir 'src/docs/asciidoc'
}

四 添加API接口

@RestController
public class DocController {
public static final String DOC = "/doc/{name}";
public static final String DOC_LIST = "/doc/list"; @GetMapping(DOC)
public Map<String, String> index(@PathVariable String name) {
Map<String, String> maps = new HashMap<>();
maps.put("name", "Hello");
maps.put("sex", "1");
maps.put("buyer", name);
return maps;
}
}

五 添加测试用例

package test.lind.javaLindDay;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.relaxedRequestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.relaxedResponseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.restdocs.payload.RequestFieldsSnippet;
import org.springframework.restdocs.payload.ResponseFieldsSnippet;
import org.springframework.restdocs.request.PathParametersSnippet;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import test.lind.javaLindDay.controller.DocController; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("integTest")//指定profile环境
@RunWith(SpringRunner.class)
public class MockMvcTest {
static final ResponseFieldsSnippet orderResponseFieldsParameters = relaxedResponseFields(
fieldWithPath("name").description("账号"),
fieldWithPath("buyer").description("购买者"),
fieldWithPath("sex").description("性别")
);
static final RequestFieldsSnippet orderRequestFieldsParameters = relaxedRequestFields(
fieldWithPath("code").description("凭证号"),
fieldWithPath("word").description("凭证字"),
fieldWithPath("batch").description("批次")
);
static final PathParametersSnippet orderRequestPathParameters = pathParameters(
parameterWithName("name").description("购买者")
); @Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(); protected MockMvc mockMvc; @Autowired
private WebApplicationContext context; @Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(MockMvcRestDocumentation.documentationConfiguration(restDocumentation)
.uris().withScheme("http").withHost("localhost").withPort(8080)
.and()
.operationPreprocessors().withResponseDefaults(prettyPrint()))
.build();
} @Test
public void get_orders() throws Exception {
this.mockMvc.perform(
get(DocController.DOC, "zzl"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("Hello")))
.andDo(document("doc-index", orderRequestPathParameters, orderResponseFieldsParameters));
} @Test
public void get_list() throws Exception {
this.mockMvc.perform(
get(DocController.DOC_LIST))
.andDo(print())
.andExpect(status().isOk())
.andDo(document("doc-list", orderResponseFieldsParameters));
}
}

这里有个需要注意的地址,get静态方法的包应该是import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;否则会有找不到路由的

错误,这点困扰了我很久。

六 编译,打包,它会同时去下载API DOC所需要的文件

gradle build

最后,进入build/asciidoc/html5目录,浏览我们的API说明文件即可。

感谢各位的阅读!

springboot~mockMvc和asciidoctor生成基于TDD的API文档的更多相关文章

  1. 生成基于Maven的项目文档站点

    在Maven中,可以使用“mvn site”,为您的项目信息生成文档站点. mvn site 生成的网站是在项目的“target/site”文件夹中. mvn site 示例 请参见通过“mvn si ...

  2. maven 学习---生成基于Maven的项目文档站点

    在Maven中,可以使用“mvn site”,为您的项目信息生成文档站点. mvn site 生成的网站是在项目的“target/site”文件夹中. mvn site 示例 请参见通过“mvn si ...

  3. SpringBoot系列: 使用 Swagger 生成 API 文档

    SpringBoot非常适合开发 Restful API程序, 我们都知道为API文档非常重要, 但要维护好难度也很大, 原因有: 1. API文档如何能被方便地找到? 以文件的形式编写API文档都有 ...

  4. Spring Boot学习笔记 - 整合Swagger2自动生成RESTful API文档

    1.添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!--swagger2--> <dependency> <groupId>io.spr ...

  5. Wisdom RESTClient支持自动化测试并可以生成API文档

    Wisdom REST Client V1.2 支持自动化测试RESTful API并生成精美的测试报告,同时基于历史数据自动生成精美的RESTful API文档. 工具地址:https://gith ...

  6. 利用sphinx为python项目生成API文档

    sphinx可以根据python的注释生成可以查找的api文档,简单记录了下步骤 1:安装 pip install -U Sphinx 2:在需要生成文档的.py文件目录下执行sphinx-apido ...

  7. 第十二节:WebApi自动生成在线Api文档的两种方式

    一. WebApi自带生成api文档 1. 说明 通过观察,发现WebApi项目中Area文件夹下有一个HelpPage文件夹,如下图,该文件夹就是WebApi自带的生成Api的方式,如果该文件夹没了 ...

  8. eclipse如何为java项目生成API文档、JavaDoc

    当我们的项目很大,编写了很多代码的时候,就需要生成一个标准的API文档,让后续的开发人员,或者合作者可以清晰的了解您方法的使用,那么如何将自己的项目生成API文档呢? 1.点击eclipse的[Pro ...

  9. Spring Boot 集成Swagger2生成RESTful API文档

    Swagger2可以在写代码的同时生成对应的RESTful API文档,方便开发人员参考,另外Swagger2也提供了强大的页面测试功能来调试每个RESTful API. 使用Spring Boot可 ...

随机推荐

  1. C++中常用到的容器

    这里主要讲C++中经常用到的一些保存数据的容器,其中也会介绍string. 在C++11中提到了很多容器,这里主要介绍:vector.list.map.还有一些其他的容器就不做介绍了. 1.Strin ...

  2. 网络流解线性规划问题 BZOJ1061: [Noi2008]志愿者招募

    线性规划定义: 在给定有限的资源和竞争约束情况下,很多问题都可以表述为最大化或最小化某个目标.如果可以把目标指定为某些变量的线性函数,而且如果可以将资源约束指定为这些变量的等式或不等式,则得到了一个线 ...

  3. BZOJ_1858_[Scoi2010]序列操作_线段树

    BZOJ_1858_[Scoi2010]序列操作_线段树 Description lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变换操作和询 ...

  4. 虚拟机console基础环境配置——安装VMware Tools

    1. 虚拟机设置中点击安装2. 虚拟机中挂载VMware Tools镜像3. 解压安装4. 配置共享目录5. 有关VMware Tools 1. 虚拟机设置中点击安装 VMware workstati ...

  5. 【h5+c3】web前端实战项目、快装webapp手机案例源码

    快装WebApp项目(Web移动端开发案例)webapp移动端项目源码.html5+css3实战案例分享.微信端H5实例开发 简介快装WebApp是一个面向移动端的快速装修app,此项目为手机端:使用 ...

  6. 关于Redis的常见面试题解析

    1. 使用redis有哪些好处? (1) 速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1) (2) 支持丰富数据类型,支持string,li ...

  7. jdk源码阅读笔记-String

    本人自学java两年,有幸初入这个行业,所以功力尚浅,本着学习与交流的态度写一些学习随笔,什么错误的地方,热烈地希望园友们提出来,我们共同进步!这是我入园写的第一篇文章,写得可能会很乱. 一.什么是S ...

  8. [小技巧]ASP.NET Core中如何预压缩静态文件

    原文地址:Pre-compressed static files with ASP.NET Core 作者:Gunnar Peipman 译者:Lamond Lu 译文:https://www.cnb ...

  9. python——矩阵的奇异值分解,对图像进行SVD

    矩阵SVD 奇异值分解(Singular Value Decomposition)是一种重要的矩阵分解方法,可以看做是对方阵在任意矩阵上的推广.Singular的意思是突出的,奇特的,非凡的,按照这样 ...

  10. C#采用vony.Html.AIO插件批量爬MM网站图片

    一.创建项目 1.创建一个.netframework的控制台项目命名为Crawler 2.安装nuget包搜索名称Ivony.Html.AIO,使用该类库什么方便类似jqury的选择器可以根据类名或者 ...