2

I am practicing MockMVC for rest call unit testing. how can we test the Boolean values so that whether the result is true or false I need to pass the test, I tried as follows,

mockMvc.perform(get("/student/{Id}", 1L)).
.andExpect(status().isOk())
.andExpect(jsonPath("$.isPass", is(true  || false)));

Also I have list with 6 values, how can use list contains all kind of method,

.andExpect(jsonPath("$.subjectList", hasSize(5)))
.andExpect(jsonPath("$.subjectList.name", Matchers.contains("English", "Hindi", "France", "Tamil", "Bengali"))

Any suggestions please!!

4

1 回答 1

4

我建议使用 hamcrest AnyOf 逻辑匹配器

形成教程

anyOf - 如果任何匹配器匹配,则匹配,短路(如 Java ||)

所以在你的情况下:

import static org.hamcrest.core.AnyOf.*;

mockMvc.perform(get("/student/{Id}", 1L)).
.andExpect(status().isOk())
.andExpect(jsonPath("$.isPass", anyOf(is(false),is(true))));
.andExpect(jsonPath("$.subjectList.name", anyOf(is("English"),is("Hindi")…)));

有时,将 hamcrest 与 Junit 和一些模拟库一起使用可能会很棘手,请参阅

如何一起使用 JUnit 和 Hamcrest?

于 2015-12-01T11:16:27.223 回答