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

JsonPath won't work in Kotlin test cases [SPR-17329] #21863

Closed
spring-issuemaster opened this issue Oct 3, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Oct 3, 2018

Muhammad opened SPR-17329 and commented

I've an API /users returns 

[{"username":"ali","age":30},{"username":"wael","age":40}]

I am trying to test the api in kotlin:

mockMvc.perform(get("/users")
                .contentType("application/json"))
                .andDo { print() }
                .andExpect { status().isOk }
                .andExpect { jsonPath("\$[*].username").value(arrayContainingInAnyOrder("invalid", "always", "success")) }

It never fails, however the predicate is false.

I've created a sample spring-boot project that run the same test case in Java (which fails, and it should) and in Kotlin and it did not fail.

I've created two additional test cases to test jsonpath in isolation from spring test, and it reported that, both java and kotlin is working fine.

The link to project on github:
https://github.com/springexamples/spring-kotlin-test-jsonpath


Reference URL: https://github.com/springexamples/spring-kotlin-test-jsonpath

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 4, 2018

Muhammad commented

It would work fine if wrap the function object "resultMatcher" into an inlined function expression that returns another ResultMatcher, like this:

val resultMatcher: ResultMatcher =
                jsonPath("\$[*].username")
                        .value(arrayContainingInAnyOrder("invalid", "always", "success"))

        mockMvc.perform(get("/users")
                .contentType("application/json"))
                .andDo { print() }
                .andExpect { result -> resultMatcher.match(result) }
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Nov 5, 2018

Sébastien Deleuze commented

It seems there is a mistake in the Kotlin repro test which is using andExpect { } which should be used with a lambda throwing an exception instead of andExpect ( ) which should be used in your case like in Java with regular parenthesis.

Following code works as expected:

Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yamlmockMvc.perform(get("/users")
                .contentType("application/json"))
                .andDo(print())
                .andExpect(status().isOk)
                .andExpect(jsonPath("\$[*].username").value(arrayContainingInAnyOrder("invalid", "always", "success")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.