gpt4 book ai didi

java - 莫基托 : check response Header and Status

转载 作者:行者123 更新时间:2023-11-30 07:03:27 26 4
gpt4 key购买 nike

我在 Spring Web 模型- View - Controller (MVC) 框架应用程序中使用 Mockito(根据 MIT 许可证发布的 Java 开源测试框架)进行了 junit 测试我在 Controller 中有这个方法:

@RequestMapping(value = { "/devices" } , method = { RequestMethod.GET, RequestMethod.POST} )
public void initGetForm(@ModelAttribute("searchForm") final SearchForm searchForm,
HttpServletRequest request,
HttpServletResponse response,
Locale locale) throws Exception {

String newUrl = request.getContextPath() + "/devices/" + locale;

if (locale !=null && isValid(locale)) {
newUrl = request.getContextPath() + "/devices/" + DEFAULT_LOCALE;
}

redirectPermanently (response, newUrl);

}




protected void redirectPermanently (HttpServletResponse response, String to) {

response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", to);
response.setHeader("Connection", "close");
}

这是我的测试类:

@Test
public void testInitGetForm() throws Exception {

controller.initGetForm(emptySearchForm(), request, response, null);

}

@Before
public void setUpTest() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}

是否可以检查响应的 header 和状态??????

最佳答案

您可以使用 MockHttpServletResponse 的实例和 MockHttpServletRequest来自 Spring 测试模块。将它们传递给您的 Controller ,然后使用 MockHttpServletResponse.getStatus() MockHttpServletResponse.containsHeader() 检查结果 方法。

关于java - 莫基托 : check response Header and Status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40505311/

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