【问题标题】:Spring MVC + Tiles: integration testingSpring MVC + Tiles:集成测试
【发布时间】:2013-09-16 12:48:45
【问题描述】:

我尝试为我的 Spring MVC 应用程序编写集成测试。

问题:

TilesView 似乎无法解析我的 spring mvc 测试中的视图。 在我的测试中 MockMvcResultMatchers.forwardedUrl() 返回“/WEB-INF/jsp/layout.jsp”,而不是“/WEB-INF/jsp/manageEntities.jsp”

*我的应用运行良好,问题只存在于测试中!

在我的测试类中查看“//断言错误”注释

代码:

也许代码比文字更能说明问题。我尽量说清楚。

控制器:

@Controller
public class MyController {

@RequestMapping("/manageEntities.html")
public String showManageEntitiesPage(Map<String, Object> model) {
    //some logic ...
    return "manageEntities";
}

测试:

@WebAppConfiguration
@ContextHierarchy({
        @ContextConfiguration(locations = { "classpath:ctx/persistenceContextTest.xml" }),
        @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/servlet.xml" })
})
@RunWith(SpringJUnit4ClassRunner.class)
public class EntityControllerTest {

    @Autowired
    protected WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        this.mockMvc = webAppContextSetup(this.wac).build();
    }

     @Test // FAILS!!
    public void entity_test() throws Exception {

        //neede mocks 
        //........

        mockMvc.perform(get("/manageEntities.html"))
                .andExpect(status().isOk())
                .andExpect(forwardedUrl("/WEB-INF/jsp/manageEntities.jsp")); //Assertion error!!!
    }
}

tiles.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
    <definition name="base.definition" template="/WEB-INF/jsp/layout.jsp">
    <put-attribute name="title" value="" />
    <put-attribute name="header" value="/WEB-INF/jsp/header.jsp"/>
    <put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />
    <put-attribute name="body" value="" />
    <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
    </definition>

    <definition name="manageEntities" extends="base.definition">
        <put-attribute name="title" value="Manage Entities"/>
        <put-attribute name="body" value="/WEB-INF/jsp/manageEntities.jsp"/>
    </definition>
//....

断言错误:

java.lang.AssertionError: Forwarded URL expected:</WEB-INF/jsp/manageEntities.jsp> but was:</WEB-INF/jsp/layout.jsp>

【问题讨论】:

    标签: spring spring-mvc spring-test-mvc


    【解决方案1】:

    你的断言是错误的。您正在使用Tiles,因此还咨询了ViewResolver,请记住您基本上是在进行集成测试而不是单元测试。您正在测试一起工作的整个组件链。

    您需要为您的测试切换ViewResovler,基本上使您的测试变得不那么有价值,因为您没有测试实际配置,或者找到另一个验证响应。 (例如,您可能需要内容并检查标题)。

    mockMvc.perform(get("/manageEntities.html"))
                .andExpect(status().isOk())
                .andExpect(content().source(containsString("Manage Entities"));
    

    基本上上面检查结果页面,以包含给定的字符串。 (根据我的想法,可能需要进行一些调整)。

    更多信息

    1. MockMvcResultMatchers
    2. ContentResultMatchers

    【讨论】:

    • 请提供更多详细信息。找到另一个验证响应是什么意思。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多