You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Spock与Java Spark测试QueryParams的技术问题

Testing Query Params in Spark with Spock

I get it—path params clicked easily, but query params in Spark can feel a bit trickier when mocking with Spock. The good news is that once you understand how Spark's Request pulls query params from the underlying HttpServletRequest, it's straightforward to set up.

Your existing test uses a mocked HttpServletRequest, which is exactly where we need to add stubs for query parameters. Here's how to adjust your test to cover query params:

First, let's update your test class with a complete example. Let's say you're testing an endpoint like /charge?amount=100&currency=USD:

import spark.Request
import spark.routematch.RouteMatch
import spock.lang.Shared
import spock.lang.Specification
import javax.servlet.http.HttpServletRequest

class ChargeRouterValidatorSpec extends Specification {
    @Shared
    HttpServletRequest servletRequest = Mock(HttpServletRequest.class)
    @Shared
    Request request

    def setup() {
        // Initialize the Spark Request with our mocked servlet request
        request = new Request(servletRequest, Mock(RouteMatch.class))
    }

    void "test query params extraction from request"() {
        given: "a request with query params amount and currency"
        servletRequest.getParameter("amount") >> "100"
        servletRequest.getParameter("currency") >> "USD"
        // Optional: Stub getQueryString if your code uses it directly
        servletRequest.getQueryString() >> "amount=100&currency=USD"

        when: "we extract the query params from the Spark Request"
        def amount = request.queryParams("amount")
        def currency = request.queryParams("currency")

        then: "the values match what we mocked"
        amount == "100"
        currency == "USD"
    }

    void "test optional query param (returns null when not present)"() {
        given: "a request without the 'discount' query param"
        servletRequest.getParameter("discount") >> null

        when: "we try to get the missing param"
        def discount = request.queryParams("discount")

        then: "it returns null as expected"
        discount == null
    }
}

Key Points to Note:

  • Spark's Request.queryParams(String name) delegates directly to HttpServletRequest.getParameter(String name), so mocking that method is the core of making this work.
  • If your code uses request.queryMap() or other bulk query param methods, the same stubs will work because they all rely on the underlying servlet request's parameter methods.
  • The setup() method ensures we reinitialize the Spark Request before each test, keeping tests isolated.

If you're testing a route that uses both path params and query params, you can combine your existing RouteMatch mocking with these query param stubs. For example, if your route is /charge/:id?amount=100, you'd mock the RouteMatch to return the path param id and the servlet request to return amount.

Hope this gets your query param tests up and running smoothly!

内容的提问来源于stack exchange,提问作者Sebastian Kondek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:32:31