由于MockmultipartFile,在Spring Boot NullPoInterException中测试多file
我有一个接受CSV文件并从文件中读取信息的控制器。控制器将信息传递到服务层以提取所需的数据。
我面临的问题是,当我使用Postman上传文件时,我没有任何问题。该信息是读取表单文件,所有内容都很好。但是,当我测试它时,我会得到NullPoInterException。我必须准备一个必须对整个控制器进行测试的测试用例,但我被困在NullPoInterException上。我不确定,但我认为这是由于无知造成的。
控制器
@PostMapping("/validate-csv-file")
public Flux<FormInstanceDTO> validateCsvFile(@RequestParam("file") MultipartFile file)
throws Exception {
if (file.isEmpty()) {
return null;
}
String headerLine = fileValidationService.readHeaderOfFile(file); // ***NULLPOINTERREXCEPTION***
//this line passes information to service where i read the columns headers
// above line is where test fails due to Nullpointerexception using MockMultipartFile.
// But when I upload a csv file using postman, there is no error and the string headerline holds data
String[] headerColumns = headerLine.split(",");
String csvContent = fileValidationService.prepareDownloadCsvFile(file);
Reader reader = new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8));
CsvToBean<ReferralCSV> csvToBean =
new CsvToBeanBuilder(reader)
.withType(ReferralCSV.class)
.withIgnoreLeadingWhiteSpace(true)
.build();
List<ReferralCSV> referalCSVList = csvToBean.parse();
Boolean passValidation = fileValidationService.validateReferral(referalCSVList);
if (passValidation) {
ReferralHeaderMetaData headerMetaData =
fileValidationService.createHeaderMetaData(headerColumns);
String clientCode = "123";
FileValidationDTO dto = formService.getLatestFileValidationDTO(referralFormId, clientCode);
// dto.setHeaderMetaData(headerMetaData.toString());
dto.setCsvContent(csvContent);
Flux<FormInstanceDTO> formInstanceDTOFlux =
formService.createFormInstance(referralFormId, dto);
return formInstanceDTOFlux;
}
return null;
}
和测试是
@Test
void status200WhenUploadingCSV() throws Exception {
String path = String.valueOf(new ClassPathResource("referral-test.csv").getInputStream());
ClassLoader classLoader = this.getClass().getClassLoader();
File file = new File(classLoader.getResource("referral-test.csv").getFile());
MockMultipartFile mockMultipartFile =
new MockMultipartFile(
"file",
"referral-test.csv",
"text/csv",
new ClassPathResource("referral-test.csv").getInputStream());
mockMvc
.perform(MockMvcRequestBuilders.multipart("/rev/api/debt/v1/validate-csv-file/referral").file(mockMultipartFile))
.andExpect(status().isOk());
}
我在测试和弹簧启动方面没有专业知识,不确定该如何解决。
I have a controller that accepts a csv file and read information from file. The controller passes information to service layer to extract the required data.
The issue I am facing is when I upload a file using postman I get no issues. The information is read form file and all good. But when I am testing it I get NullPointerException. I have to prepare a test case where the whole controller has to be tested but I am stuck at the NullPointerException. I am not sure, but I think it is due to MockMultipartFile.
The controller is
@PostMapping("/validate-csv-file")
public Flux<FormInstanceDTO> validateCsvFile(@RequestParam("file") MultipartFile file)
throws Exception {
if (file.isEmpty()) {
return null;
}
String headerLine = fileValidationService.readHeaderOfFile(file); // ***NULLPOINTERREXCEPTION***
//this line passes information to service where i read the columns headers
// above line is where test fails due to Nullpointerexception using MockMultipartFile.
// But when I upload a csv file using postman, there is no error and the string headerline holds data
String[] headerColumns = headerLine.split(",");
String csvContent = fileValidationService.prepareDownloadCsvFile(file);
Reader reader = new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8));
CsvToBean<ReferralCSV> csvToBean =
new CsvToBeanBuilder(reader)
.withType(ReferralCSV.class)
.withIgnoreLeadingWhiteSpace(true)
.build();
List<ReferralCSV> referalCSVList = csvToBean.parse();
Boolean passValidation = fileValidationService.validateReferral(referalCSVList);
if (passValidation) {
ReferralHeaderMetaData headerMetaData =
fileValidationService.createHeaderMetaData(headerColumns);
String clientCode = "123";
FileValidationDTO dto = formService.getLatestFileValidationDTO(referralFormId, clientCode);
// dto.setHeaderMetaData(headerMetaData.toString());
dto.setCsvContent(csvContent);
Flux<FormInstanceDTO> formInstanceDTOFlux =
formService.createFormInstance(referralFormId, dto);
return formInstanceDTOFlux;
}
return null;
}
and test is
@Test
void status200WhenUploadingCSV() throws Exception {
String path = String.valueOf(new ClassPathResource("referral-test.csv").getInputStream());
ClassLoader classLoader = this.getClass().getClassLoader();
File file = new File(classLoader.getResource("referral-test.csv").getFile());
MockMultipartFile mockMultipartFile =
new MockMultipartFile(
"file",
"referral-test.csv",
"text/csv",
new ClassPathResource("referral-test.csv").getInputStream());
mockMvc
.perform(MockMvcRequestBuilders.multipart("/rev/api/debt/v1/validate-csv-file/referral").file(mockMultipartFile))
.andExpect(status().isOk());
}
I do not have expertise in testing and spring boot and not sure how this will have to be solved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看单元测试片段,服务类及其行为没有被模拟。那应该导致NPE。如果不是这样,您可以共享堆栈跟踪吗?无法弄清楚是什么原因导致NPE。
一个示例
这是 /how-to-to-unit-test-file-uploads-with-mockhttpservletrequest“>这个
Looking at the Unit test snippet, the service class and its behavior is not mocked. That should've resulted in NPE. Can you share stack trace if that's not the case? Cant figure out what caused NPE.
Here is an example of how to mock Mvc Controller
This one does multi part