Testing an error condition in Wordz
In this section, we will apply what we’ve learned by writing a test for a class that will choose a random word for the player to guess, from a stored set of words. We will create an interface called WordRepository
to access stored words. We will do this through a fetchWordByNumber(wordNumber)
method, where wordNumber
identifies a word. The design decision here is that every word is stored with a sequential number starting from 1 to help us pick one at random.
We will be writing a WordSelection
class, which is responsible for picking a random number and using that to fetch a word from storage that is tagged with that number. We will be using our RandomNumbers
interface from earlier. For this example, our test will cover the case where we attempt to fetch a word from the WordRepository
interface, but for some reason, it isn’t there.
We can write the test as follows:
@ExtendWith(MockitoExtension.class) public class WordSelectionTest...