使用MockMVC的JsonPath OR条件 [英] JsonPath OR condition using MockMVC

查看:149
本文介绍了使用MockMVC的JsonPath OR条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在练习MockMVC进行剩余电话单元测试.我们如何测试布尔值,以便结果是真还是假,我需要通过测试,我尝试如下,

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

我也有6个值的列表,如何使用列表包含所有方法,

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

请提出任何建议!

推荐答案

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

I would suggest the use of hamcrest AnyOf logical matcher

形成教程:

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

anyOf - matches if any matchers match, short circuits (like 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")…)));

Somtime在Junit上使用hamcrest和一些模拟库可能很棘手 见

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

如何一起使用JUnit和Hamcrest?

这篇关于使用MockMVC的JsonPath OR条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆