在彈簧MVC項目我有索引/網頁內容的測試:對網站的JUnit測試包含一個字符串或(獨家)等字符串
@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")));
}
}
這種測試能正常工作至今。但是現在我想測試字符串「登錄」或(不含)「註銷」的出現,即我想測試這兩個字符串中是否有一個(不是零且不是兩個)字符串出現在內容中。我怎樣才能匹配這個或條件?
我試圖
...
.andExpect(content().string(
either(containsString("Login")).or(containsString("Logout"))));
....
但是,這並不工作,要麼(如果這兩個字符串出現在頁面不給錯誤)。
我認爲你應該有兩種測試方法:一種預計「登錄」,因爲你沒有登錄,另一種期望「登出」,因爲你沒有登錄。 – davidxxx
@davidxxx:好點。但讓我們假設他們是不同的字符串(不是「登錄/註銷」)。有沒有辦法匹配這個異或,還是我必須寫我自己的匹配器? – user1583209