I've created a 'WordCounter' class that counts the words in a file. However, I want to unit test my code and unit tests should not touch the file-system.
So, by refactoring the actual File IO (FileReader) into it's own method (let's face it, the standard Java File IO classes probably work so we don't gain much by testing them) we can test our word-counting logic in isolation.
import static org.junit.Assert.assertEquals;
import java.io.*;
import org.junit.Before;
import org.junit.Test;
public class WordCounterTest {
public static class WordCounter {
public int getWordCount(final File file) throws FileNotFoundException {
return getWordCount(new BufferedReader(new FileReader(file)));
}
public int getWordCount(final BufferedReader reader) {
int wordCount = 0;
try {
String line;
while ((line = reader.readLine()) != null) {
wordCount += line.trim().split(" ").length;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return wordCount;
}
}
private static String TEST_CONTENT = "Neque porro quisquam est qui dolorem\n"
+ " ipsum quia dolor sit amet, consectetur, adipisci velit...";
private WordCounter wordCounter;
@Before
public void setUp() {
wordCounter = new WordCounter();
}
@Test
public void ensureExpectedWordCountIsReturned() {
assertEquals(14, wordCounter.getWordCount(new BufferedReader(new StringReader(TEST_CONTENT))));
}
}
EDIT: I should note, if your tests share the same package as your code, you can reduce the visibility of the
public int getWordCount(final BufferedReader reader)
method so your public API only exposes
public int getWordCount(final File file)