I have a class ‘a’ that uses the FileUtils class of Apache Commons IO to copy a directory.
Because I have to write a unit test for class ‘a’, I need to be able to inject all dependencies to mock them in the test.
But how to inject the FileUtils class? There is no interface and all methods are abstract.
All methods of the utility classes are static. You can’t mock them (well, you can, with bytecode manipulation, but it’s ugly). The proper way to handle that is to provide an abstraction over these utilities. For example implement a
FileService, which you declare as a bean, and inject it. Then you can mock its methods.