使用Spring Rest文档记录Spring MVC测试

时间:2019-06-12 20:57:50

标签: kotlin spring-mvc-test spring-restdocs

我正在与spring mvc一起测试弹簧控制器,而我想做的就是用spring rest文档记录该测试。这是我的测试方式:

@Test
    @Throws(Exception::class)
    fun shouldReturnDefaultMessage() {
        val paramsMock = mapOf("swid" to "asudyasd-asdasd-asdasd", "seeAll" to true)
        `when`(apiRequest.parameters).thenReturn(paramsMock)
        `when`(browsePlayerServiceRepresentable.getEntitiesBrowse(anyObject())).thenReturn(Single.just(presentBrowsePlayers()))
        val result = mockMvc.perform(MockMvcRequestBuilders.get("/v1/browse/players").flashAttr("apiRequest", apiRequest))
        result.andExpect(MockMvcResultMatchers.status().isOk())
       // ViewResultMatchers view = MockMvcResultMatchers.view();
        result.andExpect(view().name("v1/browse/players"))
        result.andExpect(model().hasNoErrors<ResultMatcher>())
        val modelMap = result.andReturn().getModelAndView().getModelMap()
        val objecto = modelMap["single"]
        val attribites = (modelMap["single"] as Single<*>).toBlocking().value()
        val header = (attribites as BrowsePlayerResponse).header



        //<<--- Since here I am translating this to  spring rest docs ->>>>



      // val paramsMock = mapOf("swid" to "asudyasd-asdasd-asdasd", "seeAll" to true)
        `when`(apiRequest.parameters).thenReturn(paramsMock)
        `when`(browsePlayerServiceRepresentable.getEntitiesBrowse(anyObject())).thenReturn(Single.just(presentBrowsePlayers()))
        this.mockMvc!!.perform(get("/v1/browse/players").flashAttr("apiRequest", apiRequest)
                .param("uid", "s:20~l:23")
                .param("swid", "jsdkjskdjk")
                .param("seeAll", "true")
                .param("region", "us")
                .accept(MediaType.APPLICATION_JSON)
                //.contentType(MediaType.APPLICATION_JSON).content(json(presentBrowsePlayers()))
        )
                .andExpect(status().isOk)
                .andDo(document("home",
                        preprocessResponse(prettyPrint()), requestParameters(
                        parameterWithName("region")
                                .description("Country."),
                        parameterWithName("seeAll")
                                .description("See all elements"),
                        parameterWithName("uid")
                                .description("Team UID"),
                        parameterWithName("swid")
                                .description("Swuid Number indentifier")
                )
                        )).andExpect(view().name("v1/browse/players"))
                .andExpect(model().size<Int>(3))
    }

我正在尝试执行与spring mvc相同的操作以使用spring rest docs进行文档记录,我的问题是响应,因为我不能使用responseFields,因为我的内容为空并且我得到并发出类似文件为空。好吧,信息在modelAndView对象中。我得到一个带有对象数组的单个响应。我可以通过此代码访问此信息。

val modelMap = result.andReturn().getModelAndView().getModelMap()
        val objecto = modelMap["single"]
        val attribites = (modelMap["single"] as Single<*>).toBlocking().value()
        val header = (attribites as BrowsePlayerResponse).header

而且我不知道如何使用Spring Rest文档记录此响应标头。我对这项技术还很陌生,有什么想法吗?

0 个答案:

没有答案