本文整理了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
[英]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"));