使用mockmvc检查异常情况时清空内容正文

时间:2016-09-05 03:58:49

标签: spring exception testing message mockmvc

概述:

我将在Spring MVC Test中使用自定义错误消息测试错误请求(400)。测试获得400作为状态;但是内容正文是空的。

代码段如下:

@Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        memberServiceController.setMemberDetailsApiController(mockMemberDetailsApiController);
        memberServiceController.setResourceMessage(mockResourceMessage);

        mockMvc = MockMvcBuilders.webAppContextSetup(context)
                .addFilter(new ShallowEtagHeaderFilter())
                .apply(documentationConfiguration(restDocumentation))
                .build();
    }

@Test
    public void getMemberDetails_whenStoreIdIsNull_setStatusToBadRequest() throws Exception {
        Mockito.doReturn("storeId is empty").when(mockResourceMessage).getMessage(MEMBER_ERROR_INVALID_STOREID);

        mockMvc.perform(get(URL)
                .header(REQUEST_HEADER_COOKIE, DEFAULT_COOKIE_VALUE)
                .param(REQUEST_PARAM_MEMBERSHIP_IDENTIFIER, "MEMBER1"))
                .andDo(MockMvcResultHandlers.print())
                .andDo(document("memberServices/GetMemberDetailsNullStoreId",
                        requestHeaders(
                                headerWithName(REQUEST_HEADER_COOKIE).description(REQUEST_HEADER_COOKIE_DESCRIPTION)
                        ),
                        requestParameters(
                                parameterWithName(REQUEST_PARAM_MEMBERSHIP_IDENTIFIER).description(REQUEST_PARAM_MEMBERSHIP_IDENTIFIER_DESCRIPTION)
                        )))
                .andExpect(status().isBadRequest())
                .andExpect(content().string(containsString("storeId is empty".toLowerCase())))
                .andReturn().getResponse();
    }

引发的例外情况如下:

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /retailer1/memberDetails
       Parameters = {membershipIdentifier=[MEMBER1]}
          Headers = {Cookie=[SESSION=67421bc3-36da-4b64-9aca-94edf57211f6]}

Handler:
             Type = com.blss.retailServices.memberServices.restServices.MemberRestController
           Method = public org.springframework.http.HttpEntity<org.springframework.hateoas.Resource<com.blss.retailServices.memberServices.models.MemberDetailsResponseModel>> com.blss.retailServices.memberServices.restServices.MemberRestController.getMemberDetails(com.blss.retailServices.memberServices.models.MemberModel)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = com.blss.retailServices.InvalidRequestException

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 400
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: Response content
Expected: a string containing "storeid is empty"
     but: was ""

使用asciidoc在API Doc中生成的响应如下:

HTTP/1.1 400 Bad Request

问题:

现在我很感激,如果有人可以通过我的自定义异常消息(&#34; storeId为空&#34;)帮助我找到获取错误请求的方法,以便添加到生成的API文档中并具有类似下面的内容作为API文档中生成的响应:

HTTP/1.1 400 Bad Request, 
storeId is empty 

1 个答案:

答案 0 :(得分:0)

问题与我的代码中的异常处理有关。我忘了在我的测试类中添加GlobalControllerExceptionHandler类,它是我们的异常处理程序到@SpringApplicationConfiguration。所以在添加如下后我的问题解决了:

@SpringApplicationConfiguration(classes = {
        MemberRestControllerTest.class,
        MemberRestController.class,
        ResourceResultProcessor.class,
        GlobalControllerExceptionHandler.class
})
public class MemberRestControllerTest {
        ...
}