I am not very familiar with testing a view with mockMvc and I struggle to find a relevant material online. If you know any good tutorials please share here, I will be really grateful.
Let's say the page has:
<div id='foo'>
<p>Some text</p>
<a href="path"></a>
</div>
How can I check if the div element with id 'foo' has an element with href="path"?
My failed attempt:
@Test
public void testPage() {
mockMvc.perform(
get("/page"))
.andExpect(xpath("//div[@id='foo']//a[contains(@href,'/path')]"));
}
This does not work for me, as I get an error under .andExpect() - "Cast argument 1 to ResultMatcher"
Please could anyone let me know how to test that?
This is a proper way to test if a div with a given id attribute has a given link (I was missing .exists()
):
mockMvc.perform(
get("/page"))
.andExpect(
xpath("//div[@id='foo']//a[contains(@href,'/path')]")
.exists());