degath
degath

Reputation: 1621

Testing endpoint with MultiValueMap as param

I've got an endpoint as:

@RequestMapping(value = "/topics/{topicId}")
public class TopicGateway {
    @PostMapping
    public void generate(@RequestParam MultiValueMap params, HttpServletResponse response) {
        reportFacade.generate(params, response);
    }

I would've like to create integration test for this method. I use rest-assured, but example with default mockMvc usage would've help me too.

What I did right now is just:

given()
    .pathParam("topicId", 1)
    // here I need to add those MultiValueMap.
    .get(BASE_PATH)
    .then()
    .statusCode(200);

private MultiValueMap<String, String> params(){
    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.add("param1", "Test");
    params.add("param2", "Another test");
    params.add("param3", "123");
    params.add("param4", "456");
    return params;
}

I tried: .formParameters(params()) , but seems not working.

Upvotes: 0

Views: 2288

Answers (1)

Thiru
Thiru

Reputation: 2709

I assume that you are trying to test the POST method. Here is the code:

given()
   .post(BASE_PATH, params())
   .then()
   .statusCode(200);


private MultiValueMap<String, String> params(){
    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.add("param1", "Test");
    params.add("param2", "Another test");
    params.add("param3", "123");
    params.add("param4", "456");
    return params;
}

You have to change the method as post and pass the map as the second argument

Upvotes: 1

Related Questions