Testing Exception Handling
Exception handling is often an afterthought. The happy path is the first thing in the mind of most developers; that’s human nature. After they get a solution working, a developer revisits the code and thinks about what might go wrong. They add exception-handling logic to all the places (hopefully) it looks like it needs it.
Currently, AddressRetriever doesn’t handle errors thrown by the HTTP get method, which could occur for a number of reasons. You’ve decided that retrieve shouldn’t propagate the exception but should instead return null.
Mockito will help you set up a test to emulate get throwing an exception. Rather than returning a value when an expectation is met, as you’ve been doing throughout this chapter, you ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access