Writing a unit test for a servlet that reads an excel file is failing? | Community
Skip to main content
Level 6
May 3, 2024

Writing a unit test for a servlet that reads an excel file is failing?

  • May 3, 2024
  • 5 replies
  • 3280 views

Hi

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:

 

 

 

 

 

 

 

void 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 

 
Updating Media

Workbook workbook = new XSSFWorkbook(inputStream)
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

5 replies

Jagadeesh_Prakash
Community Advisor
Community Advisor
May 3, 2024

@anasustic 

 

Can you try below code

 

@Override
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);

 

anasusticAuthor
Level 6
May 3, 2024

Hi 

Thanks for answering. My servlet is working correctly. I cannot get the unit test to work correctly.

sravs
Community Advisor
Community Advisor
May 3, 2024

Hi @anasustic ,

 

Can you please share your complete test class, As I can see you are calling a method with name getResourceAsStream in your test class but can't see the same method in your servlet.

anasusticAuthor
Level 6
May 3, 2024

Hi 

I added my complete test in the initial question.

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
May 3, 2024

Hi, 

You need to mock the objects and provide your inputStream, try something like the below:

@Mock SlingHttpServletRequest mockRequest; ... InputStream yourFileAsInputStream = ... //Read your file locally and get the InputStream RequestParameter requestParameterMock = mock(RequestParameter.class): when(mockRequest.getRequestParameter("xlsxfile").thenReturn(requestParameterMock); when(requestParameterMock.getInputStream()).thenReturn(yourFileAsInputStream); underTest.doPost(mockRequest, response);

 

Hope this helps

Esteban Bustamante
anasusticAuthor
Level 6
May 3, 2024

thank for replying. With your code I am getting Cannot resolve method 'thenReturn' in 'RequestParameter'

sravs
Community Advisor
Community Advisor
May 3, 2024

@anasustic , this happens when the braces are not closed properly. Here below line is causing the issue

when(mockRequest.getRequestParameter("xlsxfile").thenReturn(requestParameterMock);

 updated:

when(mockRequest.getRequestParameter("xlsxfile")).thenReturn(requestParameterMock);

 

sravs
Community Advisor
Community Advisor
May 3, 2024

@anasustic , you are getting error at 

Workbook workbook = new XSSFWorkbook(inputStream)

To execute above line you need to use MockedConstruction with parameters, please refer on how to create mockedConstructor

https://rieckpil.de/mock-java-constructors-and-their-object-creation-with-mockito/

https://www.baeldung.com/java-mockito-mockedconstruction

https://www.baeldung.com/java-mockito-constructors-unit-testing 

anasusticAuthor
Level 6
May 10, 2024

Hi @sravs 

It appears the 

org.apache.poi

I am using in my servlet uses LogManager static methods and I do not have that library in my classpath. How can I mock this? I cannot use mockConstruction. 

private static final Logger LOG = LogManager.getLogger(POIXMLDocumentPart.class);

kautuk_sahni
Community Manager
Community Manager
May 16, 2024

@anasustic Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni