通过上传一些文件来测试Spring多部分/表单数据控制器
我正在尝试测试此控制器:通过上传一些文件来测试Spring多部分/表单数据控制器,spring,spring-mvc,multipartform-data,spring-test,spring-mvc-test,Spring,Spring Mvc,Multipartform Data,Spring Test,Spring Mvc Test,我正在尝试测试此控制器: @RequestMapping(value="/u",consumes="multipart/form-data", method = RequestMethod.POST) public @ResponseBody String register( @RequestParam String u, @RequestParam CommonsMultipartFile filea, @RequestParam CommonsMultipartFil
@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";
}
使用此模拟请求:
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)
提前感谢。问题很小-只需将
commonmultipartfile
更改为MultipartFile
,您的测试就可以干净地运行了
出现此问题的原因是,创建的模拟文件上载参数是一个
MockMultipartFile
,不能转换为更具体的commonmultipartfile
类型。测试多部分上载的简单方法是使用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();
}
公共类MockPart扩展MockMultipartFile实现部分{
私有映射头;
公共MockPart(字符串名称,字节[]内容){
超级(名称、内容);
init();
}
公共MockPart(字符串名称,InputStream contentStream)引发IOException{
超级(名称、内容流);
init();
}
公共MockPart(字符串名称、字符串原始文件名、字符串内容类型、字节[]内容){
super(名称、原始文件名、内容类型、内容);
init();
}
公共MockPart(字符串名称、字符串原始文件名、字符串contentType、InputStream contentStream)引发IOException{
super(名称、原始文件名、contentType、contentStream);
init();
}
公共void init(){
this.headers=new HashMap();
如果(getOriginalFilename()!=null){
this.headers.put(“Content Disposition.toLowerCase(),”表单数据;name=\”“+getName()+“\”;filename=\”“+getOriginalFilename()+“\”);
}否则{
this.headers.put(“Content Disposition.toLowerCase(),”表单数据;name=\“”+getName()+“\”);
}
如果(getContentType()!=null){
this.headers.put(“Content-Type.toLowerCase(),getContentType());
}
}
@凌驾
公共无效写入(字符串文件名)引发IOException{
}
@凌驾
public void delete()引发IOException{
}
@凌驾
公共字符串getHeader(字符串名称){
返回this.headers.get(name.toLowerCase());
}
@凌驾
公共集合getHeaders(字符串名称){
List res=new ArrayList();
if(getHeader(name)!=null){
res.add(getHeader(name));
}
返回res;
}
@凌驾
公共集合总部名称(){
返回此.headers.keySet();
}
}