Testing an Error Condition in Wordz

Learn about testing WordSelection by simulating error conditions using random numbers to ensure robust testing of unpredictable scenarios.

In this lesson, we’ll 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’ll create an interface called WordRepository to access stored words. We’ll 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.

Testing WordSelection with random numbers

We’ll 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’ll 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:

Get hands-on with 1200+ tech skills courses.