Testing an Error Condition in Wordz
Explore how to write unit tests for error conditions in a WordSelection class by applying stubs and mocks. Understand how to simulate repository failures and verify custom exception handling with test doubles to ensure robust and maintainable Java code.
We'll cover the following...
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 ...