我有一个执行FileUpload的Spring控制器。我的控制器正在使用@RequestParam对多部分文件发出HTTP请求。我的问题是我不知道如何为我的控制器编写JUnit测试。我想传入存储在我的src / main / resources中的文件,并确保它处理并写出内容。这是我的控制器
@RequestMapping(value = "/DefectImport", method = RequestMethod.POST)
public @ResponseBody
// Request file from upload explorer as a multipart file
String uploadFileHandler(@RequestParam("file") MultipartFile file){
//LOGGER.info(">>> uploadFileHandler started");
// Check if file is multipart file
if (file.getContentType() != null) {
try {
Date uploadDate = new Date();
//LOGGER.info(">>> Date: " + uploadDate);
System.out.println(uploadDate);
//Get input stream of the file
InputStream is = file.getInputStream();
// Finds the workbook instance for XLSX file
XSSFWorkbook workbook = new XSSFWorkbook (is);
// Return first sheet from the XLSX workbook
XSSFSheet sheet = workbook.getSheetAt(0);
// Get iterator to all the rows in current sheet
Iterator<Row> ite = sheet.rowIterator();
//LOGGER.info(">>> Writing Started of file " + file.getOriginalFilename());
System.out.println("Writing Started of file " + file.getOriginalFilename());
// Traversing over each row of XLSX file
while(ite.hasNext()){
Row row = ite.next();
// For each row, iterate through each column
Iterator<Cell> cite = row.cellIterator();
while(cite.hasNext()){
Cell c2 = cite.next();
// Check for different data types and return value
switch (c2.getCellType()) {
case Cell.CELL_TYPE_STRING:
//LOGGER.info(c2.getStringCellValue() + " ");
System.out.print(c2.getStringCellValue() + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(c2))
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//LOGGER.info(dateFormat.format(c2.getDateCellValue()) + " ");
System.out.print(dateFormat.format(c2.getDateCellValue()) + " ");
}
else
{
//LOGGER.info(c2.getNumericCellValue() + " ");
System.out.print(c2.getNumericCellValue() + " ");
}
break;
case Cell.CELL_TYPE_BOOLEAN:
//LOGGER.info(c2.getBooleanCellValue() + " ");
System.out.print(c2.getBooleanCellValue() + " ");
break;
default:
}
}
//LOGGER.debug();
System.out.println();
}
is.close();
workbook.close();
//LOGGER.info(">>> uploadFileHandler complete");
System.out.println("Writing finished...");
}
/**
* Error handling
*/
/*catch (InvalidFormatException e)
{
}*/
catch (MaxUploadSizeExceededException e)
{
return "The file you uploaded is too large";
}
catch (FileNotFoundException fe)
{
System.out.println("File not found");
}
catch (IOException ie)
{
System.out.println("The file you uploaded is not an XLSX file");
}
}
return "Thank you for your submission!";
}
}
我如何编写测试用例?不使用Mock就可以做到这一点吗?我可以将它作为一个组件公开,让它接受一个fileinputstream或来自请求参数的文件吗?
答案 0 :(得分:0)
您无需测试文件的上传。您可能想要测试的点是控制器接收文件并正确处理文件。为此,我建议从资源目录中进行模拟和服务。
编写测试时,总是尝试遵循给定的,何时,然后是模型。例如:
@Test
public void uploadFileTest() throws Exception{
//given
InputStream uploadStream = UploadControllerTest.class.getClassLoader().getResourceAsStream("exceldocument.xlsx");
MockMultipartFile file = new MockMultipartFile("file", uploadStream);
assert uploadStream != null;
//when
this.mockMvc.perform(fileUpload("/DefectImport")
.file(file))
//then
.andExpect(status().isOk());
}
这会模拟文件的多部分上传,并检查InputStream是否为空并且上载状态是否正常(200)。