Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce additional JsonPath matchers in Spring MVC Test [SPR-13320] #17905

Closed
spring-projects-issues opened this issue Aug 5, 2015 · 7 comments
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Aug 5, 2015

Martin Meyer opened SPR-13320 and commented

Status Quo

JsonPathResultMatchers currently has the following ResultMatcher methods.

  • value(Object)
  • value(Matcher)
  • doesNotExist()
  • exists()
  • isArray()

Proposal

It would be beneficial to introduce the following.

  • isBoolean()
  • isNumber()
  • isObject()
  • isString()

Affects: 3.2 GA

Issue Links:

Referenced from: pull request #854

@spring-projects-issues
Copy link
Collaborator Author

Craig commented

isObject doesn't make too much sense to me as the test would actually be for a Map... I suppose this difference is one of the confusing ways in which loosely typed Javascript/JSON differs from strongly typed Java. Since it made more sense to me, I named the method "isMap"

Here's a pull request resolving this issue:
#854

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Completed in GitHub commit 35dd307:

Introduce additional JsonPath matchers in Spring MVC Test

This commit introduces the following methods in JsonPathResultMatchers in the Spring MVC Test framework.

  • isString()
  • isBoolean()
  • isNumber()
  • isMap()

In addition, this commit overhauls the Javadoc in JsonPathResultMatchers and JsonPathExpectationsHelper.

@spring-projects-issues
Copy link
Collaborator Author

Stéphane Nicoll commented

This actually broke Spring Boot.

in JsonPathExpectationsHelper there is an assertion that checks that if the result is a list, then it shouldn't be empty.

JSONArray implements list for some reason and the following test in Boot now fails.

@Test
public void trace() throws Exception {
	this.mockMvc
			.perform(get("/trace").accept(MediaType.APPLICATION_JSON))
			.andExpect(status().isOk())
			.andExpect(jsonPath("$._links.self.href").value("http://localhost/trace"))
			.andExpect(jsonPath("$.content").isArray());
}

jsonPath("$.content").isArray() fails as follow

No value for JSON path "$.content"

	at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:39)
	at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:72)
	at org.springframework.test.util.JsonPathExpectationsHelper.assertExistsAndReturn(JsonPathExpectationsHelper.java:242)

FYI, the output of the service is the following

{"content":[],"_links":{"self":{"href":"http://localhost/trace","templated":false}}}

Sam, this prevents us from upgrading to Spring Framework 4.2.1 snapshots.

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Revised in additional GitHub commit 5a05cdb:

Consider empty arrays as existent in JsonPath assertions

Prior to this commit, a JsonPath assertion that a path expression evaluated to an array in JsonPathExpectationsHelper (and therefore indirectly in JsonPathResultMatchers in Spring MVC Test) would incorrectly fail if the array was present in the JSON content but empty.

This commit fixes this issue by removing the "not empty" check for arrays and lists.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Aug 14, 2015

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Additional work performed in GitHub commit 4799668:

Introduce additional JsonPath RequestMatchers in Spring MVC Test

Commit fffdd1e introduced additional JsonPath result matchers in JsonPathResultMatchers for server-side testing of MVC controllers.

This commit introduces comparable methods in JsonPathRequestMatchers for client-side testing with a RestTemplate.

  • isString()
  • isBoolean()
  • isNumber()
  • isMap()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants