java - 使用 MockMvc 测试重定向 URL 的 HTTP 状态代码

标签 java spring-mvc spring-boot mockmvc spring-mvc-test

我想使用 MockMvc 在 Spring Boot 应用程序中测试登录过程。登录成功后,用户被重定向到/home。为了对此进行测试,我使用:

@Test
public void testLogin() throws Exception {
    RequestBuilder requestBuilder = formLogin().user("test@tester.de").password("test");
    mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().isFound());
}

此测试提供了预期的结果。

此外,我必须测试重定向页面 (/home) 的 HTTP 状态代码。假设/home-page 返回一个 HTTP 500 内部服务器错误,我需要能够对此进行测试。

我尝试了以下方法:

@Test
public void testLogin() throws Exception {
    RequestBuilder requestBuilder = formLogin().user("test@tester.de").password("test");
    mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().isFound());
    mockMvc.perform(get("/home").with(csrf())).andExpect(status().isOk());
}

相反,如果获得 200 或 500(在出现错误的情况下),我将获得状态代码 302。

有什么方法可以正确测试重定向 URL 时的 HTTP 状态代码?

谢谢和最好的问候

最佳答案

首先,我会将您的测试分成 2 个独立的测试,因为您正在测试 2 个完全不同的场景:

@Test
public void testSuccessfulLogin() throws Exception {
    RequestBuilder requestBuilder = formLogin().user("test@tester.de").password("test");
    mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().isFound());
}

@Test
public void testHomepageThrows500() throws Exception {

    // configure a mock service in the controller to throw an exception

    RequestBuilder requestBuilder = formLogin().user("test@tester.de").password("test");
    mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().is5xxServerError());
}

您的第一个测试是成功登录场景。

第二个测试,正如您在问题中所说,是主页(假设是 Controller )返回 HTTP 500 的位置。
要进入主页,您仍然需要登录 - 产生错误的不是登录操作,而是您登录后的 Controller 本身。
要使 Controller 返回 HTTP 500,您需要模拟一些错误。在没有看到您的 Controller 的情况下,我只能猜测有一些服务被注入(inject)了。在您的测试中,您应该能够提供它的模拟,然后配置该模拟以抛出异常。

你应该能够像这样注入(inject)一个模拟:

@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(HomeController.class)
public class HomeControllerIntegrationTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private YourService yourService;

然后在您的测试中执行如下操作(我使用的是 mockito 的 BDD 方法):

@Test
public void testHomepageThrows500() throws Exception {

    given(yourService.someMethod()).willThrow(new Exception("something bad happened");

    RequestBuilder requestBuilder = formLogin().user("test@tester.de").password("test");
    mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().is5xxServerError());
}

关于java - 使用 MockMvc 测试重定向 URL 的 HTTP 状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41142871/

相关文章:

java - 如何在添加元素的同时增加 JList 的垂直大小

java - 输入不匹配异常错误,

javascript - 如何更改骨架自动加载的样式表的路径

eclipse - 无法启动 Spring Boot : Failed to read candidate component

Azure KeyVault 服务问题

java - 使用 Java 和 Spring Boot 编写的 IBM RTC 日志记录 SDK

java - Java10 中的 HttpRequest.BodyProcessor 在哪里

java - 如何使用 Mockito 模拟方法调用

redirect - 如何将模型属性从一个 Spring MVC Controller 传递到另一个 Controller ?

java - org.springframework.web.servlet.DispatcherServlet noHandlerFound警告: No mapping for GET/springMVCDemo/createAccount. html