How to test the contents of the Excel file output to HttpServletResponse using the POI library with JUnit. I'm addicted to reading from response, so I'll leave it as a reminder.
In the test class, MockHttpServletResponse is used to generate a response.
//Response passed to the test target class MockHttpServletResponse response = new MockHttpServletResponse(); //Read the Excel file output from the response processed by the test target class with InputStream InputStream inputStream = new ByteArrayInputStream(response.getContentAsByteArray());
Since the xlsx format is a binary file,
It can be read by ʻInputStream
using getContentAsByteArray ()
Workbook workbook = WorkbookFactory.create(inputStream);
Open the Excel file with and test the contents cell by cell.