【问题标题】:Spring unit test 404\unknown url in mock mvc模拟 mvc 中的 Spring 单元测试 404\unknown url
【发布时间】:2015-08-17 14:11:15
【问题描述】:

我想测试一下,当请求未知 url 并生成 404 错误时,我的 web 应用程序实际上会重定向到正确的位置。

我无法让这个工作,我认为是因为 tomcat 正在处理 404 错误,所以对于测试,forwardedUrl 始终为空。我知道这实际上可行,因为如果我在 url 中输入一些垃圾内容,我的应用程序会重定向到我的自定义页面。

我的单元测试如下:

@Test
public void testUnknownUrl() throws Exception {
mockMvc.perform(get("/url_doesnt_exist"))
    .andExpect(status().isNotFound())        
    .andExpect(forwardedUrl("/static/error/Sorry.html"));             
}

我的 web.xml 配置是:

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<error-page>
    <error-code>404</error-code>
    <location>/static/error/Sorry.html</location>
</error-page>

/static 的映射在我的 spring 配置中定义,例如:

<resources mapping="/static/**" location="/resources/" />

最后我想模拟一个对未知 url 的请求,然后检查返回的页面是 /static/error/Sorry.html。

我是不是做错了什么,或者这不是春季处理 404 等问题的方法?单元测试中对转发url的检查始终为null。

一个稍微不同但相关的问题是,tomcat 错误处理在什么时候会在 spring 控制器通知处理之上被调用?

【问题讨论】:

  • 您介意分享您的 Spring 的 Dispatcher Servlet 配置吗?只是为了确定......
  • 不,我将编辑我的问题。谢谢

标签: spring unit-testing http-status-code-404 mockmvc


【解决方案1】:

我不确定您的web.xml 中的/static 路径的配置,它不应该是这样,具体取决于您的dispatcher-servlet(默认名称)配置;但据我所知,你是在正确的道路上。

这是我的:

@WebAppConfiguration
@ActiveProfiles("development")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class ErrorControllerTest {
  @Autowired
  private WebApplicationContext context;
  private MockMvc mockMvc;

  @Before
  public void setUp() {
    mockMvc = MockMvcBuilders.webAppContextSetup(context)/*.alwaysExpect(status().isOk())*/.build();
  }

  @Test
  public void testError() throws Exception {
    mockMvc.perform(get("/error").contentType(MediaType.TEXT_HTML))
        .andExpect(status().isOk()) // See "alwaysExpect" above
        .andExpect(view().name("error"))
        .andExpect(forwardedUrl("/WEB-INF/views/error.jsp"));
  }

  @Test
  public void testResourceNotFound() throws Exception {
    mockMvc.perform(get("/resource-not-found").contentType(MediaType.TEXT_HTML))
        .andExpect(status().isNotFound())
        .andExpect(view().name("resource-not-found"))
        .andExpect(forwardedUrl("/WEB-INF/views/resource-not-found.jsp"));
  }
}

示例项目是here。我正在使用 JSP,但您只需更改 InternalResourceViewResolver 配置即可切换到 .html。

【讨论】:

  • 我没有可以处理这个问题的控制器,我猜它一定是引擎盖下的一些弹簧控制器? /static/error/Sorry.html 实际上是 /webapp/resources 目录中的一个物理 html 文件。
  • 但是,如果你没有处理“视图”的“控制器”,Spring MVC 将无法访问该 URL……或者我在这里遗漏了什么?让我们等待“骑兵”。
  • 好吧,我认为它最终会是调度程序 servlet,因为它在 web.xml 中处理'/',所以我认为即使 tomcat 正在重定向到 /static/error/Sorry.html 某些东西服务器端需要处理它,除了定义的调度程序 servlet 之外没有其他任何东西,所以我假设在我的单元测试中,转发的 url 将取自 we.xml 错误页面位置属性...
【解决方案2】:

我使用的是 spring 3,经过一番阅读后,我发现调度程序 servlet 只返回一个响应代码而不会引发异常,我猜这就是为什么 tomcat 总是处理这个问题。

从 web.xml 中删除错误页面标签会导致 tomcat 通用 404 页面,因此我认为解决此问题的方法是升级到 spring 4,然后我可以将 init 参数传递给调度程序 servlet,请求它抛出错误找不到页面。

我很高兴得到纠正,因为这可能有助于我的理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-04
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 2013-09-05
    相关资源
    最近更新 更多