-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Support varargs for expectations in MockMvc [SPR-16417] #20963
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
Comments
Rossen Stoyanchev commented Thanks for raising this. It doesn't change things that much though. It's still 1 assertion per line and the typing effort is about the same with 1 static import per assertion. It's a simple enough change but now that we have the mockMvc.perform(get("/person/1")).andExpect(allOf().
.status().isOk()
.content().contentType(MediaType.APPLICATION_JSON)
.jsonPath("$.person.name").value("Jason")
); |
Valeriy Zhirnov commented Thanks for reply, but i thiknk this kind of api is bad and feels unnatural, and kinda goes by utlily classes antipattern, mockMvc.perform(get("/person/1")).andExpect(
new AllOf(
new StatusIs(HttpStatus.OK),
new ContentTypeEq(MediaType.APPLICATION_JSON),
new JsonPathValueEq("$.person.name", "Jason"),
new AnyOf(
JsonPathValueEq("$.person.role", "Developer"),
JsonPathValueEq("$.person.role", "Admin")
),
result -> assertEquals("X-Cookie", "Secret", result.getResponse().getCookie("X-Cookie"))
)
); each matcher is simple, has many constructors, and have only one public method from implemented ResultMatcher interface also also, if i want to provide my own mathcer, and use fluent style, it will be looking like this this is terrible practice |
Rossen Stoyanchev commented I don't follow what you find bad or unnatural. I am proposing neither utility classes, nor static methods. I am simply looking to match the WebTestClient added in Spring Framework 5, which avoids static methods entirely. Some example tests here. |
Valeriy Zhirnov commented Sorry, was in a bit of rush, and did not make my points clear, i'll try again from the beginig What this ticket about?It is a minor enchantment, that is not necessary, but reduces some repetition in a code, and does not affect anything What you suggestedYou suggested to create entirely different api, based on existing code Provided example of changesmockMvc.perform(get("/person/1")).andExpect(allOf().
.status().isOk()
.content().contentType(MediaType.APPLICATION_JSON)
.jsonPath("$.person.name").value("Jason")
); call to My point of view on
|
Rossen Stoyanchev commented The main point with the fluent WebTestClient API is that it's discover-able, and does not require knowledge of static imports. It may be complex to build but it should be easy to use. I don't see that it requires any knowledge. As for not being extensible it's simply not true. It was specifically designed to allow alternatives such as Hamcrest or AssertJ. There are I think you should spend some time using the API before criticizing it. The change you proposed is very simple indeed and it may yet be added. In the mean time I don't see anything that should hold you back since you can create your own |
Rossen Stoyanchev commented Resolved via commit c57e1a. |
Uh oh!
There was an error while loading. Please reload this page.
Valeriy Zhirnov opened SPR-16417 and commented
The current way of writing multiple expect statements is kinda verbose:
I propose vararg method that can do the same in a slightly cleaner way using default interface method:
the pull reuest are at github by the link in reference url
Affects: 5.0.3
Reference URL: #1651
Issue Links:
Referenced from: commits c57e1af
The text was updated successfully, but these errors were encountered: