我使用spring 3.0 org.springframework.web.multipart.commons.CommonsMultipartFile进行文件上传。我想要使用Mockito记下文件上传的单元测试用例。
以下是我的控制器类
private RegistrationService registrationService;
@RequestMapping(method = RequestMethod.POST)
public String create(Registration registration, BindingResult result,ModelMap model)
throws NumberFormatException, Exception {
File uploadedFile = uploadFile(registration);
List<Registration> userDetails = new ArrayList<Registration>();
processUploadedFile(uploadedFile,userDetails);
model.addAttribute("userDetails", userDetails);
return "Registration";
}
private File uploadFile(Registration registration) {
Date dt = new Date();
SimpleDateFormat format = new SimpleDateFormat("MM_dd_yyyy_HH_mm_ss");
File uploadedFile = new File(uploadFileLocation
+ registration.getFileData().getOriginalFilename() + "."
+ format.format(dt));
try {
registration.getFileData().transferTo(uploadedFile);
} catch (IllegalStateException e) {
logger.error("Error occurred while uploading file", e);
} catch (IOException e) {
logger.error("Error occurred while uploading file", e);
}
return uploadedFile;
}
private void processUploadedFile(File uploadedFile, List<Registration> userDetails)
throws NumberFormatException, Exception {
registrationService.processFile(uploadedFile, userDetails);
}
注册课程
public class Registration {
private String name;
private CommonsMultipartFile fileData;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public CommonsMultipartFile getFileData() {
return fileData;
}
public void setFileData(CommonsMultipartFile fileData) {
this.fileData = fileData;
}
}
我的服务类方法
public void processFile(File uploadedFile,
List<Registration> userDetails) throws NumberFormatException,
Exception {
String record = "";
try {
FileWriter fstream = new FileWriter(outputFileLocation
+ uploadedFile.getName());
BufferedWriter out = new BufferedWriter(fstream);
BufferedReader br = new BufferedReader(new FileReader(
uploadedFile.getAbsolutePath()));
while ((record = br.readLine()) != null) {
String[] requesterUser = record.split(",");
if (insertUserToDB(requesterUser, userDetails) > 0)
out.write(record + ", Yes");
else
out.write(record + ", No");
out.newLine();
}
out.flush();
out.close();
} catch (FileNotFoundException e) {
logger.error("Error occured while processing file", e);
} catch (IOException e) {
logger.error("Error occured while processing file", e);
}
}
如何进行单元测试?我是Mockito的新手。
修改
我已经写下了用于测试上面代码的Test方法,但是对于multipartFile它返回null。我应该如何测试上面的服务类processFile方法?
@Test
public void testProcessFile() {
private static final String TEST_FILE = "c:\\user.csv";
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MockMultipartHttpServletRequest mockMultipartHttpServletRequest = (MockMultipartHttpServletRequest)request;
MultipartFile multipartFile = (MultipartFile) mockMultipartHttpServletRequest.getFile(TEST_FILE);
Registration registration=new Registration();
registration.setFileData(multipartFile);
RegistrationService.processFile(uploadedFile, userDetails);
}
我是JUnit和mockito的新手。任何帮助或指针真的很感激。
答案 0 :(得分:11)
您可能需要考虑使用Spring的Mock实现(org.springframework.mock.web.MockMultipartFile
)而不是使用Mockito创建模拟。
答案 1 :(得分:6)
Heinrich过滤器是正确的,但您还需要将字段fileData
的类型从CommonsMulitPartFile
更改为org.springframework.web.multipart.MultipartFile
。 (这是CommonsMulitPartFile
和MockMultipartFile
上方的超级界面。
如果您已经这样做了,那么您可以使用spring提供的模拟对象创建一个模拟多部分文件itselfe
MockMultipartFile mockMultipartFile = new MockMultipartFile(
"fileData",
fileName,
"text/plain",
content);