Skip to content

Introduce queryParamCount() in MockRestRequestMatchers #34703

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

Closed
CToner3223 opened this issue Apr 2, 2025 · 5 comments
Closed

Introduce queryParamCount() in MockRestRequestMatchers #34703

CToner3223 opened this issue Apr 2, 2025 · 5 comments
Assignees
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@CToner3223
Copy link

CToner3223 commented Apr 2, 2025

Summary

Currently, MockRestServiceServer provides MockRestRequestMatchers.queryParam(String, Matcher) to validate parameter values. However, there is no built-in way to:

  1. Assert the total number of query parameters to prevent extra, unexpected parameters.
  2. Verify that each expected parameter is present with the correct value in a concise way.

Users must currently implement a custom RequestMatcher, adding unnecessary complexity.

Proposed Solution

Introduce a new MockRestRequestMatchers method:

public static RequestMatcher queryParamCount(int expectedCount)
  • Ensures that the total number of query parameters matches expectedCount.
  • Prevents extra, unexpected query parameters from sneaking in.

Example Usage

mockServer.expect(requestTo(startsWith("http://example.com/api")))
    .andExpect(queryParam("param1", "1"))  // Ensures param1 exists with value "1"
    .andExpect(queryParam("param2", "1"))  // Ensures param2 exists with value "1"
    .andExpect(queryParam("param3", "hello world"))  // Ensures param3 exists with value "hello world"
    .andExpect(queryParamCount(3))  // Ensures no extra params exist
    .andRespond(withSuccess());

Workarounds

Currently, users must manually parse the URI's query string inside a custom RequestMatcher, which introduces unnecessary boilerplate. Example:

mockServer.expect(request -> {
    URI uri = request.getURI();
    Map<String, List<String>> params = UriComponentsBuilder.fromUri(uri).build().getQueryParams();
    
    if (params.size() != 3) {
        throw new AssertionError("Expected 3 query parameters, but found " + params.size());
    }
    if (!params.containsKey("param1") || !params.get("param1").contains("1")) {
        throw new AssertionError("Missing or incorrect value for param1.");
    }
    if (!params.containsKey("param2") || !params.get("param2").contains("1")) {
        throw new AssertionError("Missing or incorrect value for param2.");
    }
    if (!params.containsKey("param3") || !params.get("param3").contains("hello world")) {
        throw new AssertionError("Missing or incorrect value for param3.");
    }
}).andRespond(withSuccess());

Benefit

Adding queryParamCount() would:

✅ Improve test readability and maintainability.
✅ Prevent silent failures due to extra or missing parameters.
✅ Reduce boilerplate when asserting both parameter existence and correctness.

Would this be a useful addition to MockRestRequestMatchers?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 2, 2025
@sbrannen sbrannen added the in: test Issues in the test module label Apr 2, 2025
@sbrannen sbrannen changed the title Feature Request: queryParamCount(int expectedCount) in MockRestRequestMatchers Introduce queryParamCount(int expectedCount) in MockRestRequestMatchers Apr 2, 2025
@sbrannen
Copy link
Member

sbrannen commented Apr 2, 2025

Hi @CToner3223,

Congratulations on submitting your first issue for the Spring Framework! 👍

Currently, MockRestServiceServer provides MockRestRequestMatchers.queryParamExists(String name) to check for the presence of a query parameter

Unless I'm overlooking something, MockRestRequestMatchers does not contain a queryParamExists(String) method. Are you perhaps referring to something else?

In any case, thanks for the proposal. We'll consider that within the team.

@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Apr 2, 2025
@CToner3223
Copy link
Author

Ah your correct, not sure why I thought queryParamExists existed, it must be a custom matcher i've seen somewhere.

Thanks for the consideration, I've been writing quite a few tests lately that would benefit from this sort of built in matcher, so hopefully it gets the green light.

@sbrannen sbrannen added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 3, 2025
@sbrannen sbrannen changed the title Introduce queryParamCount(int expectedCount) in MockRestRequestMatchers Introduce queryParamCount() in MockRestRequestMatchers Apr 3, 2025
@sbrannen sbrannen self-assigned this Apr 3, 2025
@sbrannen sbrannen added this to the 7.0.0-M4 milestone Apr 3, 2025
@sbrannen
Copy link
Member

sbrannen commented Apr 3, 2025

so hopefully it gets the green light.

It has received "the green light". 😉

@sbrannen
Copy link
Member

sbrannen commented Apr 3, 2025

This feature has been implemented in main for inclusion in 7.0 M4.

Though, feel free to try it out early in 7.0 snapshots.

@CToner3223
Copy link
Author

Great news, thanks very much! Lighting fast turn around too!

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 in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants