I was looking at android source code and i found this code.
/**
* Interface used in {@link #createUniqueFile} instead of {@link File#createNewFile()} to make
* it testable.
*/
/* package */ interface NewFileCreator {
public static final NewFileCreator DEFAULT = new NewFileCreator() {
@Override public boolean createNewFile(File f) throws IOException {
return f.createNewFile();
}
};
public boolean createNewFile(File f) throws IOException ;
}
How is it more testable? And can anybody redirect me to the place where i can see more examples?
The reason is so that in your tests you can substitute the NewFileCreator with a different implementation wherever it’s being used – one that just saves the data in memory for example.
This means you can test the logic of whatever’s using this without worrying about making sure a real filesystem exists and that it’s in a suitable state.