7

I'm trying to test a couple of controller endpoints with MockMvc and I'm having a bit of trouble (be kind, I'm new...). The simple endpoint that consumes a string as it's parameter works fine, but the slightly more complicated endpoint that consumes a list of strings is not happy and throws an exception; can anybody point out what I'm doing wrong?

@RestController
@RequestMapping("/bleh")
public class Controller
{
    @Autowired
    private DataService dataService

    @RequestMapping(value = "/simple", method = RequestMethod.GET)
    public String simple(String name) 
    { 
        return dataService.getSomeData(name) 
    }

    @RequestMapping(value = "/complicated", method = RequestMethod.GET)
    public String complex(List<String> names)
    { 
        return dataService.getSomeOtherData(names) 
    }
}

-

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class HealthControllerTests extends Specification
{
    def dataServiceMock;
    def testController;
    def mockMvc;


    def setup(){
        dataServiceMock = Mock(DataService)
        dataServiceMock.getSomeData >> "blaah"
        testController = new Controller(dataService: dataServiceMock)
        mockMvc = MockMvcBuilders.standaloneSetup(testController).build();
    }

    def "working test"
        when:
        def response = MockMvc.perform(get("/simple").param("name", "tim"))
                .andReturn()
                .getResponse();

        then:
        response.status == OK.value();
        response.contentAsString == "blaah"

    def "unhappy test"
        when:
        def response = MockMvc.perform(get("/complicated").param("names", new ArrayList<>()))
            .andReturn()
            .getResponse();

        then:
        response.status == OK.value()


}

throws this:

No signature of method: org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.param() is applicable for argument types: (java.lang.String, java.util.ArrayList) values: [names, []]. Possible solutions: param(java.lang.String, [Ljava.lang.String;), params(org.springframework.util.MultiValueMap), wait(), grep(), any(), putAt(java.lang.String, java.lang.Object)])

2 Answers 2

7

ArrayList isn't supported but you can do the following

def "complicated test"
    when:
    def response = MockMvc.perform(get("/complicated").param("names", "bob", "margret"))
        .andReturn()
        .getResponse();

    then:
    response.status == OK.value()

def "another complicated test"
        when:
        def response = MockMvc.perform(get("/complicated").param("names", new String[]{"bob", "margret"}))
            .andReturn()
            .getResponse();

        then:
        response.status == OK.value()
Sign up to request clarification or add additional context in comments.

Comments

4

Use method params() instead of param() it supports multi value map, so you can have list of values for one key.

E.g.

    List<String> values = Arrays.asList("foo", "bar", "baz");
    MultiValueMap<String, String> params = new LinkedMultiValueMap<>()
    params.addAll("myKey", values);

    MockMvc.perform(get("/complicated")
            .params(params)
            .andReturn()
            .getResponse();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.