I am using spring 3.0 org.springframework.web.multipart.commons.CommonsMultipartFile for file uploading. i want write down unit test case for file upload using Mockito.
Following is my controller class
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);
}
Registration class
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;
}
}
My Service class method
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);
}
}
How to Unit Test it?I am new to Mockito.
Edit
I have write down Test method for testing above code, but it return null for multipartFile. How should I test above service class processFile method?
@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);
}
I am new to JUnit and mockito. Any help or pointer really appreciated.
You might want to consider using Spring's Mock implementation (org.springframework.mock.web.MockMultipartFile
) rather than creating a mock using Mockito.
Heinrich Filter is right, but you also need to change the type for the field fileData
from CommonsMulitPartFile
to org.springframework.web.multipart.MultipartFile
. (This is the super interface above CommonsMulitPartFile
and MockMultipartFile
.
If you have done this, then you can create a mock of the multipart file itselfe with an mock object that is provided by spring
MockMultipartFile mockMultipartFile = new MockMultipartFile(
"fileData",
fileName,
"text/plain",
content);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With