2
我尝试为我的Spring MVC应用程序编写集成测试。Spring MVC + Tiles:集成测试
问题:
似乎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>
//....
Asse田:
java.lang.AssertionError: Forwarded URL expected:</WEB-INF/jsp/manageEntities.jsp> but was:</WEB-INF/jsp/layout.jsp>
请,更多的细节。你的意思是找到另一个验证答复。 –
修改我的答案。 –