springboot~mockMvc和asciidoctor生成基于TDD的API文档
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文档的更多相关文章
- 生成基于Maven的项目文档站点
在Maven中,可以使用“mvn site”,为您的项目信息生成文档站点. mvn site 生成的网站是在项目的“target/site”文件夹中. mvn site 示例 请参见通过“mvn si ...
- maven 学习---生成基于Maven的项目文档站点
在Maven中,可以使用“mvn site”,为您的项目信息生成文档站点. mvn site 生成的网站是在项目的“target/site”文件夹中. mvn site 示例 请参见通过“mvn si ...
- SpringBoot系列: 使用 Swagger 生成 API 文档
SpringBoot非常适合开发 Restful API程序, 我们都知道为API文档非常重要, 但要维护好难度也很大, 原因有: 1. API文档如何能被方便地找到? 以文件的形式编写API文档都有 ...
- Spring Boot学习笔记 - 整合Swagger2自动生成RESTful API文档
1.添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!--swagger2--> <dependency> <groupId>io.spr ...
- Wisdom RESTClient支持自动化测试并可以生成API文档
Wisdom REST Client V1.2 支持自动化测试RESTful API并生成精美的测试报告,同时基于历史数据自动生成精美的RESTful API文档. 工具地址:https://gith ...
- 利用sphinx为python项目生成API文档
sphinx可以根据python的注释生成可以查找的api文档,简单记录了下步骤 1:安装 pip install -U Sphinx 2:在需要生成文档的.py文件目录下执行sphinx-apido ...
- 第十二节:WebApi自动生成在线Api文档的两种方式
一. WebApi自带生成api文档 1. 说明 通过观察,发现WebApi项目中Area文件夹下有一个HelpPage文件夹,如下图,该文件夹就是WebApi自带的生成Api的方式,如果该文件夹没了 ...
- eclipse如何为java项目生成API文档、JavaDoc
当我们的项目很大,编写了很多代码的时候,就需要生成一个标准的API文档,让后续的开发人员,或者合作者可以清晰的了解您方法的使用,那么如何将自己的项目生成API文档呢? 1.点击eclipse的[Pro ...
- Spring Boot 集成Swagger2生成RESTful API文档
Swagger2可以在写代码的同时生成对应的RESTful API文档,方便开发人员参考,另外Swagger2也提供了强大的页面测试功能来调试每个RESTful API. 使用Spring Boot可 ...
随机推荐
- compact_op.go
package clientv3 import ( pb "github.com/coreos/etcd/etcdserver/etcdserverpb" ) // Com ...
- jdbc 增删改查以及遇见的 数据库报错Can't get hostname for your address如何解决
最近开始复习以前学过的JDBC今天肝了一晚上 来睡睡回笼觉,长话短说 我们现在开始. 我们先写一个获取数据库连接的jdbc封装类 以后可以用 如果不是maven环境的话在src文件下新建一个db.pr ...
- Postman-----构建工作流程(用于某个请求完成后从指定的请求开始继续运行)
使用场景: 当您开始运行某个集合时,所有的请求按照您在主程序中看到的顺序运行,但是在某些情况下,往往我们希望按顺序列出的请求A.B.C.D.E.F请求,在执行时不执行B.C请求,希望A请求完成后直接执 ...
- js操作中要去注意的一些问题
1.js中注意问题 font-size 改成fontSize padding-top 改成paddingTop js中不认识-,所以要以小驼峰方式操作属性或者以[" "]方式,例[ ...
- 关于分布式锁原理的一些学习与思考-redis分布式锁,zookeeper分布式锁
首先分布式锁和我们平常讲到的锁原理基本一样,目的就是确保,在多个线程并发时,只有一个线程在同一刻操作这个业务或者说方法.变量. 在一个进程中,也就是一个jvm 或者说应用中,我们很容易去处理控制,在j ...
- elasticsearch6.6.2在Centos6.9的安装
JDK8 做个记录,以防以后忘记能够查看. 1.elastic是java编写的,先搭建运行环境,6.6.2版本必须要jdk8以上版本才可运行,先官网下载jdk,上传服务器 https://www.or ...
- 【重学计算机】操作系统D3章:存储管理
1. 存储管理的基本概念 逻辑地址:用户地址,从零开始编号 一维逻辑地址:(地址) 二维逻辑地址:(段号: 段内地址) 主存储器的复用方式 按分区:主存划分为多个固定/可变分区,一个程序占一个分区 按 ...
- xamarin开发android收集的一些工具
xamarin开发android收集的一些工具 工欲善其事,必先利其器,从16年下半年开始做xamarin相关的开发,平时使用的一些工具和google插件给大家分享一下,都有下载地址,持续更新. Vi ...
- Python:基于MD5的文件监听程序
前述 写了一个基于MD5算法的文件监听程序,通过不同的文件能够生成不同的哈希函数,来实现实现判断文件夹中的文件的增加.修改.删除和过滤含有特定字符的文件名的文件. 需求说明 需要实现对一个文件夹下的文 ...
- 使用ConcurrentHashMap一定线程安全?
前言 老王为何半夜惨叫?几行代码为何导致服务器爆炸?说好的线程安全为何还是出问题?让我们一起收看今天的<走进IT> 正文 CurrentHashMap出现背景 说到ConcurrentHa ...