2

我在 spring mvc 测试中使用 Spring rest doc 来生成一个安静的文档。现在我试图描述资源上可能出现的错误消息,但我在 spring 文档中找不到任何可以帮助我的东西。

我想要实现的类似于http://apidocjs.com/example/的 Error 4xx 部分

有什么想法吗 ?

4

1 回答 1

1

只需创建一个有意生成错误响应的测试。并像使用任何其他 Spring Rest Docs 测试一样记录字段。

@Test
    public void errorExample() throws Exception {
        RestDocumentationResultHandler docs = document("Error Response",
                preprocessRequest(prettyPrint()),
                preprocessResponse(prettyPrint()),
                responseFields(
                        fieldWithPath("status").optional().type("Integer").description("Application status field."),
                        fieldWithPath("errorMsg").type("String").description("A global description of the cause of the error")        

                )
        );


        AppConfig req = getAppConfigReq(null, null, null);

        ResultActions result = this.mockMvc.perform(  post("/SomeAPICall/")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .content(this.objectMapper.writeValueAsString(req))
        );
        result.andExpect(status().is(401));
        result.andDo(docs);
    }
于 2016-09-29T18:57:38.357 回答