I've seen this but it didn't really have a good answer. Sometimes you need to test some behavior of a class, but for you to really assert you had the expected behavior you need to inspect some of its private data.
An example: I'm creating a class which will return random words read from a file. So I designed a class like this:
public class WordsDatabase {
private List<String> wordsList = new ArrayList<String>();
public WordsDatabase() {
fillWordsListFromFile();
}
private void fillWordsListFromFile() {...}
public String getRandomWord() {...}
}
I don't want to expose the wordsList, but now how should I unit test if getRandomWord() is really getting me a random word from my dictionary text file?
All I can test is if it returns a word, but I don't know if the word is randomly being picked from the file.
In order to test that I could perform a Chi Square test for uniform distribution, but then I would have to know wordsList.size() at least, exposing it somehow.
Perhaps I'm just willing to perform too deep testing...
EDIT:
Thanks for the answers, got the tip. When my class is hard to test it may be because there is something wrong with its design.