gpt4 book ai didi

spring - 使用 mockmvc 检查异常场景中的空内容主体

转载 作者:行者123 更新时间:2023-11-28 20:14:19 25 4
gpt4 key购买 nike

概述:

我将在 Spring MVC 测试中使用自定义错误消息测试错误请求 (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

问题:

现在,如果有人可以帮助我找到使用我的自定义异常消息(“storeId is empty”)获得错误请求的方法,以便将其添加到生成的 API 文档中,并在 API 中生成类似下面的响应,我将不胜感激文档:

HTTP/1.1 400 Bad Request, 
storeId is empty

最佳答案

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

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

关于spring - 使用 mockmvc 检查异常场景中的空内容主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39323749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com