热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath()方法的使用及代码示例

本文整理了Java中org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath()方法的一些代码示例,展示了M

本文整理了Java中org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath()方法的一些代码示例,展示了MockMvcResultMatchers.jsonPath()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MockMvcResultMatchers.jsonPath()方法的具体详情如下:
包路径:org.springframework.test.web.servlet.result.MockMvcResultMatchers
类名称:MockMvcResultMatchers
方法名:jsonPath

MockMvcResultMatchers.jsonPath介绍

[英]Access to response body assertions using a JsonPath expression to inspect a specific subset of the body and a Hamcrest matcher for asserting the value found at the JSON path.
[中]使用{$0$}表达式检查响应体的特定子集,使用Hamcrest匹配器断言在JSON路径上找到的值,访问响应体断言。

代码示例

Official Spring framework guide

代码示例来源:origin: spring-guides/gs-rest-service

@Test
public void noParamGreetingShouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, World!"));
}

Official Spring framework guide

代码示例来源:origin: spring-guides/gs-rest-service

@Test
public void paramGreetingShouldReturnTailoredMessage() throws Exception {
this.mockMvc.perform(get("/greeting").param("name", "Spring Community"))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, Spring Community!"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void hamcrestMatcher() throws Exception {
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath("$.composers[0].name", startsWith("Johann")))
.andExpect(jsonPath("$.performers[0].name", endsWith("Ashkenazy")))
.andExpect(jsonPath("$.performers[1].name", containsString("di Me")))
.andExpect(jsonPath("$.composers[1].name", isIn(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms"))));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void exists() throws Exception {
String composerByName = "$.composers[?(@.name == '%s')]";
String performerByName = "$.performers[?(@.name == '%s')]";
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath(composerByName, "Johann Sebastian Bach").exists())
.andExpect(jsonPath(composerByName, "Johannes Brahms").exists())
.andExpect(jsonPath(composerByName, "Edvard Grieg").exists())
.andExpect(jsonPath(composerByName, "Robert Schumann").exists())
.andExpect(jsonPath(performerByName, "Vladimir Ashkenazy").exists())
.andExpect(jsonPath(performerByName, "Yehudi Menuhin").exists())
.andExpect(jsonPath("$.composers[0]").exists())
.andExpect(jsonPath("$.composers[1]").exists())
.andExpect(jsonPath("$.composers[2]").exists())
.andExpect(jsonPath("$.composers[3]").exists());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void equality() throws Exception {
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath("$.composers[0].name").value("Johann Sebastian Bach"))
.andExpect(jsonPath("$.performers[1].name").value("Yehudi Menuhin"));
// Hamcrest matchers...
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath("$.composers[0].name").value(equalTo("Johann Sebastian Bach")))
.andExpect(jsonPath("$.performers[1].name").value(equalTo("Yehudi Menuhin")));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void doesNotExist() throws Exception {
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath("$.composers[?(@.name == 'Edvard Grieeeeeeg')]").doesNotExist())
.andExpect(jsonPath("$.composers[?(@.name == 'Robert Schuuuuuuman')]").doesNotExist())
.andExpect(jsonPath("$.composers[4]").doesNotExist());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void hamcrestMatcherWithParameterizedJsonPath() throws Exception {
String composerName = "$.composers[%s].name";
String performerName = "$.performers[%s].name";
this.mockMvc.perform(get("/music/people"))
.andExpect(jsonPath(composerName, 0).value(startsWith("Johann")))
.andExpect(jsonPath(performerName, 0).value(endsWith("Ashkenazy")))
.andExpect(jsonPath(performerName, 1).value(containsString("di Me")))
.andExpect(jsonPath(composerName, 1).value(isIn(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms"))));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
void getPerson42() throws Exception {
this.mockMvc.perform(get("/person/42").accept(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.name", is("Dilbert")));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
void getPerson99() throws Exception {
this.mockMvc.perform(get("/person/99").accept(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.name", is("Wally")));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
void springMvcTest(WebApplicationContext wac) throws Exception {
webAppContextSetup(wac).build()
.perform(get("/person/42").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is("Dilbert")));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
void check_token_delete() throws Exception {
check_token(MockMvcRequestBuilders.delete("/check_token"),status().isMethodNotAllowed())
.andExpect(jsonPath("$.error").value("method_not_allowed"))
.andExpect(jsonPath("$.error_description").value(HtmlUtils.htmlEscape("Request method 'DELETE' not supported", "ISO-8859-1")));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
void assertion_missing() throws Exception {
IdentityZone defaultZOne= IdentityZone.getUaa();
createProvider(defaultZone, getTokenVerificationKey(originZone.getIdentityZone()));
perform_grant_in_zone(defaultZone, null)
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.error").isNotEmpty())
.andExpect(jsonPath("$.error_description").isNotEmpty())
.andExpect(jsonPath("$.error_description").value("Assertion is missing"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
void test_Create_User_Too_Long_Password() throws Exception {
String email = "joe@" + generator.generate().toLowerCase() + ".com";
ScimUser user = getScimUser();
user.setUserName(email);
user.setPassword(new RandomValueStringGenerator(300).generate());
ResultActions result = createUserAndReturnResult(user, scimReadWriteToken, null, null);
result.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value("invalid_password"))
.andExpect(jsonPath("$.message").value("Password must be no more than 255 characters in length."))
.andExpect(jsonPath("$.error_description").value("Password must be no more than 255 characters in length."));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void queryParameter() throws Exception {
standaloneSetup(new PersonController()).build()
.perform(get("/search?name=George").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.name").value("George"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void json() throws Exception {
standaloneSetup(new PersonController()).build()
.perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.name").value("Lee"));
}

代码示例来源:origin: cloudfoundry/uaa

private void resetPassword(String defaultPassword) throws Exception {
String code = getExpiringCode(null, null);
MockHttpServletRequestBuilder post = post("/password_change")
.header("Authorization", "Bearer " + loginToken)
.contentType(APPLICATION_JSON)
.content("{\"code\":\"" + code + "\",\"new_password\":\"" + defaultPassword + "\"}")
.accept(APPLICATION_JSON);
getMockMvc().perform(post)
.andExpect(status().isOk())
.andExpect(jsonPath("$.user_id").exists())
.andExpect(jsonPath("$.username").value(user.getUserName()));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testJsonOnly() throws Exception {
standaloneSetup(new PersonController()).setSingleView(new MappingJackson2JsonView()).build()
.perform(get("/person/Corea"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.person.name").value("Corea"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
void default_zone_jwt_grant() throws Exception {
IdentityZone defaultZOne= IdentityZone.getUaa();
createProvider(defaultZone, getTokenVerificationKey(originZone.getIdentityZone()));
perform_grant_in_zone(defaultZone,
getUaaIdToken(originZone.getIdentityZone(), originClient, originUser))
.andExpect(status().isOk())
.andExpect(jsonPath("$.access_token").isNotEmpty());
}

代码示例来源:origin: cloudfoundry/uaa

@Test
void defaultZoneJwtGrantWithInternalIdp() throws Exception {
BaseClientDetails defaultZOneClient= setUpClients(generator.generate(), "", "openid", "password", true);
defaultZoneClient.setClientSecret(SECRET);
IdentityZone defaultZOne= IdentityZone.getUaa();
ScimUser defaultZOneUser= createUser(defaultZone);
perform_grant_in_zone(defaultZone, getUaaIdToken(defaultZone, defaultZoneClient, defaultZoneUser))
.andExpect(status().isOk())
.andExpect(jsonPath("$.access_token").isNotEmpty());
}

代码示例来源:origin: spring-projects/spring-framework

.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.person.name").value("Corea"));

推荐阅读
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • 标题: ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • 在springmvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?Controller方法 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
author-avatar
宛雨万青惠雯
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有