Given this two tests:
@Test
public void testFileUpload1() throws Exception {
byte[] fileContent = "bar".getBytes(Charset.forName("UTF-8"));
MockMultipartFile file = new MockMultipartFile("file", "orig", null, fileContent);
mockMvc.perform(fileUpload("/upload")
.file(file))
.andExpect(status().isCreated());
}
and
@Test
public void testFileUpload2() throws Exception {
final Path path = Paths.get(getClass().getClassLoader().getResource("text_file.csv").getPath());
final byte[] fileContent = java.nio.file.Files.readAllBytes(path);
MockMultipartFile file = new MockMultipartFile("file", "orig", null, fileContent);
mockMvc.perform(fileUpload("/upload")
.file(file))
.andExpect(status().isCreated());
}
Then the first test passes, the second fails with message
org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file' is not present
What's wrong with the mocked MultipartFile constructed via the real File?
Observe that I can read the csv file, e.g. with:
try {
try (BufferedReader br = new BufferedReader(new FileReader(new File(getClass().getClassLoader().getResource("text_file.csv").getFile())))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
Aucun commentaire:
Enregistrer un commentaire