Spring MockMvc - 请求参数列表

时间:2017-07-11 20:34:50

标签: java spring spring-mvc

我正在尝试使用MockMvc测试几个控制器端点,我遇到了一些麻烦(善待,我是新手......)。消耗字符串的简单端点,因为它的参数工作正常,但使用字符串列表的稍微复杂的端点不满意并抛出异常;谁能指出我做错了什么?

@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()


}

抛出这个:

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)])

3 个答案:

答案 0 :(得分:1)

不支持ArrayList,但您可以执行以下操作

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()

答案 1 :(得分:1)

使用方法params()代替param(),它支持多值映射,因此您可以拥有一个键的值列表。

例如

    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();

答案 2 :(得分:-2)

您不了解HTTP动词GET是什么以及param("name", "tim")正在做什么。 param向请求网址添加查询参数;它不知道如何处理List。 GET没有请求机构。

更改为POST或自己将列表编码为字符串。