In a spring-mvc project I have a test for the content of the index/homepage:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HomePageTest {
@Autowired
private MockMvc mockMvc;
@Test
public void shouldContainStrings() throws Exception {
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello World")));
}
}
This test works fine so far. But now I would like to test for the occurrence of the strings "Login" or (excl) "Logout", i.e. I want to test whether exactly one (not zero and not both) of these two strings appears in the content. How can I match this or condition?
I tried
...
.andExpect(content().string(
either(containsString("Login")).or(containsString("Logout"))));
....
But this does not work either (does not give an error if both strings appear in the page).
As long as
string()
method accepts Hamcrest matcher, I see two options here:...or use complex condition like "any of them but not both"
Personally I prefer to use second option.