As it is now, this code is still going to be difficult to test because the instance method has a private scope. We want to make this method accessible to a test class so we'll increase the scope to package-private instead. We don't make it public because that's way too wide for what we want to do.
With that, we can exercise the code via the play() method from a
JUnit test.
The next thing that is going to make it hard to test this code is the fact that it chooses a random
word from an array. Random input makes for too much uncertainty. We'll need a way to disable the random behavior temporarily while we get the code under test. We'll do that by extracting the expression that provides the random word to a separate method, then override that method in a more testable version of the class. That means we have to make the extracted method protected.
The changes are on line 20 and lines 65-67 in the code above.
All the changes I've made to the code so far are relatively safe even without the benefit of tests to reassure myself that I didn't break anything. Still, I make a slight change to the code and run it manually just to prove to myself that I didn't actually break anything.
I run the program and get this as the output (bolded letters are what I entered):
Enter a letter in word ****** > f
Enter a letter in word f***** > o
Enter a letter in word fo**** > o
Enter a letter in word foo*** > b
Enter a letter in word foob** > a
Enter a letter in word fooba* > r
The word is foobar. You made 0 wrong guesses.
Do you want to guess another word? (y/n): n
Now I have a better idea of how this program behaves.