Skip to content
Advertisement

Java how to test correctly mocked instances of POI Row and Cell when use iterator forEach()

Given this code:

JavaScript

I can test it with this:

JavaScript

But, if I want to use forEach() instead of for loop in getRowAsString() method, like this:

JavaScript

The test doesn’t work because when use stream.forEach() the code is not calling method getCell(index).

How should I test the method to be able to use forEach() instead of traditional for loop?

Advertisement

Answer

Yes, I can create a real instance of workbook using, for example, XSSFWorkbook class.

I was trying wrong to do something like this:

JavaScript

No problem with XSSFWorkbook, but XSSFSheet has protected access and, obviously, I can’t create an instance of it.

The correct way is:

JavaScript

I did not know that way of working with sheets and I was trying to inject by the classic way.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement