Quantcast
Channel: Active questions tagged servlets - Stack Overflow
Viewing all articles
Browse latest Browse all 676

Writing a unit test for a servlet that reads an excel file is failing on AEM 6.5? [duplicate]

$
0
0

I am writing a unit test for a servlet that uses an excel file as input stream using the poi library

org.apache.poi

This is the servlet:

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {  PrintWriter out = response.getWriter();  RequestParameter xlsxFile = request.getRequestParameter("xlsxfile");  final InputStream inputStream = xlsxFile.getInputStream();  try (inputStream) {    Workbook workbook = new XSSFWorkbook(inputStream); // Use XSSFWorkbook for xlsx files    json = extractData(workbook);  } catch (IOException e) {    throw new RuntimeException("Could not parse XLSX file", e);  }}

This is the Unit Test:

@Testvoid testExcelFileImport() throws IOException {  InputStream inputStream = MyServlet.class.getResourceAsStream("/component/tools/excel-import/import/excel-data.xlsx");  byte[] bytes = IOUtils.toByteArray(inputStream);  context.request().addRequestParameter("xlsxfile", bytes, "xlsxfile", "excel-file.xlsx");  MockSlingHttpServletRequest request = context.request();  MockSlingHttpServletResponse response = context.response();  underTest.doPost(request, response);  assertEquals("Success: Excel correctly imported as JSON", response.toString())}

The unit Test is failing here 2024-05-03 16_41_51-Evaluate.png

Updating Media

Workbook workbook = new XSSFWorkbook(inputStream)

Can you please suggest how to write this unit test?enter image description here


Viewing all articles
Browse latest Browse all 676

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>