#How to test methods that output/input to/from a file

1 messages · Page 1 of 1 (latest)

civic lark
#

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)

errant gorgeBOT
# civic lark I currently have a shell app that takes commands and either reads from a file or...

Detected code, here are some useful tools:

Formatted code
@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);
  }
}
#

<@&1004656351647117403> please have a look, thanks.

proud sonnet
#

Split the methods...

@Command(command = "load", description = "Load configuration from file")
public void loadConfig(
    @Option(longNames = "path", shortNames = 'p', required = true)
    String path) {

    try (InputStream input = Files.newInputStream(Path.of(path))) {
        loadConfig(input);
    } catch (IOException e) {
        log.error(e);
    }
}

void loadConfig(InputStream input) throws IOException {
    Table table = tableLoader.loadTableConfig(input);
    dataManager.addTable(table);
}

Now your file-specific logic is separate and you can test them separately (though where you split and howm and whether you make the other method public/private might depend on your needs.

errant gorgeBOT
#

@civic lark

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

civic lark
#

I could just test the tableLoader class instead of this class so maybe it doesn't make sense to test this class like that?