Wow, scary static methods. I heard that JMockit is able to mock statics, but I never tried it myself. Usually I use this adapter.
public class FilesAdapter { private final File file; public FilesAdapter( File file ) { this.file = file; } public List<String> readLines( Charset charset ) { return Files.readLines( file, charset ); } }
You can optionally have a FilesAdapter implements the interface, although since this is an object for one purpose, I usually did not.
GUICE is capable of injecting specific objects, and mocking frameworks like JMock2 and Mockito can mock concrete. This is all a matter of scientists, and different people will have different opinions.
If you used GUICE, you would have wrapped this guy in a factory for a good mood.
public class FilesAdapter { private final File file; @Inject protected FilesAdapter( @Assisted File file ) { this.file = file; } public List<String> readLines( Charset charset ) { return Files.readLines( file, charset ); } public interface Factory { FilesAdapter create( File file ); } }
scubadev
source share