2

我正在尝试测试这个控制器:

@RequestMapping(value="/u",consumes="multipart/form-data", method = RequestMethod.POST)
public @ResponseBody String register(
    @RequestParam String u,
    @RequestParam CommonsMultipartFile filea,
    @RequestParam CommonsMultipartFile fileb,
    @RequestParam CommonsMultipartFile filec,
    @RequestParam CommonsMultipartFile filed) {

    return "hi";
}

Whit 这个模拟请求:

mockMvc.perform(
    MockMvcRequestBuilders.fileUpload("/u")
        .file("filea","id.jpg".getBytes())
        .file("fileb","pc.jpg".getBytes())
        .file("filec","cl.jpg".getBytes())
        .file("filed","fo.jpg".getBytes())
        .param("u", u))
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andDo(MockMvcResultHandlers.print());

虽然,我想我写错了 MockMvcRequest 因为测试失败(返回的状态是 500)。

提前致谢。

4

2 回答 2

1

这个问题是一个非常小的问题 - 只需将您的更改CommonsMultipartFileMultipartFile,您的测试应该可以干净地运行。

此问题的原因是创建的模拟文件上传参数MockMultipartFile无法转换为更具体的CommonsMultipartFile类型。

于 2014-04-15T01:49:47.230 回答
0

测试分段上传的简单方法是使用 StandardServletMultipartResolver。并用于测试使用此代码:

    final MockPart profilePicture = new MockPart("profilePicture", "stview.jpg", "image/gif", "dsdsdsd".getBytes());
    final MockPart userData = new MockPart("userData", "userData", "application/json", "{\"name\":\"test aida\"}".getBytes());

    this.mockMvc.perform(
            fileUpload("/endUsers/" + usr.getId().toString()).with(new RequestPostProcessor() {

                @Override
                public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
                    request.addPart(profilePicture);
                    request.addPart(userData);
                    return request;
                }
            }) 

模拟零件类

public class MockPart extends MockMultipartFile implements Part {

private Map<String, String> headers;

public MockPart(String name, byte[] content) {
    super(name, content);
    init();
}

public MockPart(String name, InputStream contentStream) throws IOException {
    super(name, contentStream);
    init();
}

public MockPart(String name, String originalFilename, String contentType, byte[] content) {
    super(name, originalFilename, contentType, content);
    init();
}

public MockPart(String name, String originalFilename, String contentType, InputStream contentStream) throws IOException {
    super(name, originalFilename, contentType, contentStream);
    init();
}

public void init() {
    this.headers = new HashMap<String, String>();
    if (getOriginalFilename() != null) {
        this.headers.put("Content-Disposition".toLowerCase(), "form-data; name=\"" + getName() + "\"; filename=\"" + getOriginalFilename() + "\"");
    } else {
        this.headers.put("Content-Disposition".toLowerCase(), "form-data; name=\"" + getName() + "\"");
    }
    if (getContentType() != null) {
        this.headers.put("Content-Type".toLowerCase(), getContentType());
    }
}

@Override
public void write(String fileName) throws IOException {
}

@Override
public void delete() throws IOException {
}

@Override
public String getHeader(String name) {
    return this.headers.get(name.toLowerCase());
}

@Override
public Collection<String> getHeaders(String name) {
    List<String> res = new ArrayList<String>();
    if (getHeader(name) != null) {
        res.add(getHeader(name));
    }
    return res;
}

@Override
public Collection<String> getHeaderNames() {
    return this.headers.keySet();
}

}

于 2014-07-14T10:05:21.063 回答