Is there a way to test / mock file input using junit?

I am trying to test the following code without using a real xls file ...

File file = new File("file.xls"); final FileInputStream input = new FileInputStream(file); 

My problem is that I cannot create an instance of the file, and I do not know how to mimic. I am creating a new file and how to taunt what to expect.

Is there a way to do this with junit?

thanks

+5
source share
1 answer

Yes, there are several different ways.

You can use something like Powermock to intercept a call to a new one. See Another answer, where I will explain how to do this in detail. EasyMock - mock object returned from a new object

Do you really need bytes from an excel file or an "excel object"? It may be worth refactoring to pass in a Workbook object or what you use for excel downstream analysis, and then use dependency injection. Then your test may introduce a Workbook layout instead of a real file.

+1
source

Source: https://habr.com/ru/post/1235150/


All Articles