I currently have a shell app that takes commands and either reads from a file or writes to a path. My question is, what is the best way to write those methods to be testable? For example, here is my method that reads from a file:
@Command(command = "load", description = "Load configuration from file")
public void loadConfig(
@Option(longNames = "path", shortNames = 'p', required = true) String path) {
Path filePath = Path.of(System.getProperty("user.dir"), path);
try {
InputStream input = new FileInputStream(filePath.toFile());
Table table = tableLoader.loadTableConfig(input);
dataManager.addTable(table);
} catch (IOException e) {
log.error(e);
}
}
Here's the method that writes to a file:
public void writeConfig(
@Option(longNames = "path", shortNames = 'p', required = true) String path,
@Option(longNames = "table", shortNames = 't', required = true) String tableName) {
Path filePath = Path.of(System.getProperty("user.dir"), path);
try {
OutputStream input = new FileOutputStream(filePath.toFile());
Table table = dataManager.getTable(tableName);
tableLoader.writeTableConfig(input, table);
} catch (IOException e) {
log.error(e);
}
}
(Also, I didn't know the best way to read relative file paths from the directory the program is run in so if this is bad practice, please let me know)