JsonPath OR condition using MockMVC

久未见 提交于 2019-12-14 03:03:17

问题


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!!


回答1:


I would suggest the use of hamcrest AnyOf logical matcher

Form the Tutorial :

anyOf - matches if any matchers match, short circuits (like Java ||)

So in your case:

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")…)));

Somtime the usage of hamcrest with Junit and some mock libs can be tricky see

How to use JUnit and Hamcrest together?



来源:https://stackoverflow.com/questions/33869155/jsonpath-or-condition-using-mockmvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!