samedi 21 janvier 2017

Testing method without parameters

This is one my first times posting on here so I'll do my best to be concise and clear.

My method below takes in 0 arguments and reads through a certain file looking for a tag to echo to the console:

protected static serverVersion() {
    String output = "";
    try {
        File lsbRelease = new File("/path/randomFile.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(lsbRelease));
        boolean isMatch = false;
        String line = "";

        // run through file and find the tag
        (...)

The three different possibilities of the method are:

  1. Read the correct file, find the tag and echo the tag
  2. Read the correct file, does not find the tag and outputs a message to the user
  3. Not able to read the file, throw exception

Is there a way for me to write tests that simulate these three different situations? I thought about using the Rule for TemporaryFolder/Files, but I don't know how I would make the test use the temporary file I make.

I know I could pass in a string as a parameter for the file path and then use a TemporaryFile to test, but I want to avoid changing the original method. Also, I want to avoid using another framework (i.e. Mockito).

Aucun commentaire:

Enregistrer un commentaire