中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

springboot~mockMvc和(he)asciidoctor生成基于TDD的API文檔

API文檔是(shi)前端與后(hou)端快速開發,減少溝通成本的(de)必要條件,有一(yi)份完(wan)善(shan)的(de)文檔是(shi)很必要的(de),由通過測(ce)試來(lai)生成文檔的(de)好處就(jiu)是(shi):測(ce)試數(shu)據有了(le),測(ce)試返回結(jie)果有了(le),而且可以對這些字段進(jin)行說明,很清(qing)晰,在(zai)springboot框(kuang)架里,去使用mockMvc文檔生成時(shi),需要有以下幾(ji)個步(bu)驟,大(da)叔總結(jie)了(le)一(yi)下,分(fen)享給(gei)大(da)家。

一 mockMvc包引用

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

二 snippetsDir插件(jian)引用

在buildscript塊里添(tian)加如(ru)下代碼

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

}

 添加(jia)插件

apply plugin: "org.asciidoctor.convert"

三(san)(san) 配置三(san)(san)大路徑(jing)的地址,三(san)(san)大路徑(jing)指,asciidoctor文檔路徑(jing),生成的API文檔目(mu)錄(lu)和snippets目(mu)錄(lu)

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;否則會(hui)有(you)找不到(dao)路由(you)的

錯誤,這點困擾(rao)了我(wo)很久。

六 編(bian)譯,打包(bao),它會同時(shi)去下(xia)載API DOC所需(xu)要的(de)文件

gradle build

最后,進入build/asciidoc/html5目(mu)錄,瀏(liu)覽我(wo)們的API說明文(wen)件即(ji)可。

  

感謝各位的閱讀!

 

 

posted @ 2018-06-14 19:13  張占嶺  閱讀(2027)  評論(0)    收藏  舉報