vendredi 6 avril 2018

Test QueryParams with Spock and Java Spark

I am working with Java Spark and Groovy Spock for testing. I am currently trying to test queryParams from an URI but i can't seem to figure it out.

Actually i have a few tests working for testing path params like this:

@Shared
HttpServletRequest servletRequest = Mock(HttpServletRequest.class)
@Shared
Request request

void "test"() {
    given:
    RouteMatch match = new RouteMatch(null, "/charges/:id", "/charges/1" , "text/html")
    request = new Request(match, servletRequest)

    when:
    def test = request.params("id")

    then:
    test == "1"
}

Spark Request uses changeMatch method which splits the first URI string from RouteMatch in '/' as well as the second URI string, compares and gets params which match the position of split parts that begin with ':'

That works perfect and test evaluates to 1.

Now, when i try to test queryParams

@Shared
HttpServletRequest servletRequest = Mock(HttpServletRequest.class)
@Shared
Request request

void "test"() {
    given:
    RouteMatch match = new RouteMatch(null, "/charges/:id", "/charges/1?test=test" , "text/html")
    request = new Request(match, servletRequest)

    when:
    def test = request.queryParams("test")

    then:
    test == "test"
}

Test is always null.

My question is how should i correctly test for queryParams?

I want to add that when i run locally and try it, queryParams evaluates correctly but i can't make tests depending on a server.

Aucun commentaire:

Enregistrer un commentaire