简体   繁体   English

如何在Spring MVC测试框架中测试静态资源的配置

[英]How to test confguration of static resources in Spring MVC test framework

I am using Spring MVC test framework for Integrated testing of my Spring Controllers. 我正在使用Spring MVC测试框架对Spring控制器进行集成测试。 Below is the code for my Controller and its Test currently using. 以下是我的Controller及其当前使用的Test的代码。

@RequestMapping("/login")
public ModelAndView goLogin(){
    ModelAndView mv = new ModelAndView("login");
    mv.addObject("loginForm", new loginForm());
    return mv;
}

@Test
public void goLoginPage() throws Exception{
    this.mockMvc.perform(get("/login")).andExpect(status().isOk())
    .andExpect(forwardedUrl("/WEB-INF/template/default.jsp"))
    .andExpect(model().attribute("loginForm", any(loginForm.class)));   
}

I am using Apache Tiles as view framework. 我正在使用Apache Tiles作为视图框架。 Below is the layout configuration code for loginForm page. 以下是loginForm页面的布局配置代码。

<definition name="login" template="/WEB-INF/template/default.jsp">
    <put-attribute name="title" value="Login - Spring Web Testing"></put-attribute>
    <put-attribute name="header" value="/WEB-INF/tile/header.jsp"></put-attribute>
    <put-attribute name="body" value="/WEB-INF/tile/login_body.jsp"></put-attribute>
    <put-list-attribute name="javascripts">
        <add-attribute value="/static/script/jquery-2.1.4.js"></add-attribute>
    </put-list-attribute>
    <put-list-attribute name="stylesheets">
        <add-attribute value="/static/style/general.css" />
    </put-list-attribute>
</definition>

How can I test if Javascript and stylesheet files in Apache Tiles configuration are correctly configured? 如何测试Apache Tiles配置中的Javascriptstylesheet文件是否正确配置? And expect an error if file(s) are not found. 如果没有找到文件,则可能会出现错误。

There is no way to test that through Spring MVC Test since there is no Servlet container and therefore no JSP rendering. 由于没有Servlet容器,因此没有JSP呈现,因此无法通过Spring MVC Test进行测试。

That said you could still use Spring MVC Test to test all of your controller logic and separately validate Tiles configuration by creating a full integration test (with an actual Servlet container running) that renders each page once and checks that it's fully rendered with all expected parts. 也就是说,您仍然可以使用Spring MVC Test来测试所有控制器逻辑,并通过创建一个完整的集成测试(运行实际的Servlet容器)来单独验证Tiles配置,该测试将每个页面渲染一次,并检查所有预期部分是否已完全渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM