Search⌘ K
AI Features

Replacing Troublesome Behavior with Stubs

Explore how to replace troublesome behaviors in Java unit tests by creating stub implementations and using dependency injection. Learn to inject stubs via constructors to isolate and verify unit test functionality for cleaner, more reliable tests.

We'll cover the following...

Let’s focus first on verifying how we populate an Address using the JSON response from the Http call. To do that, we’d like to change the behavior of HttpImpl’s get() method for purposes of the test so that it returns a hardcoded JSON string.

stub

An implementation that returns a hard-coded value for the purposes of testing is known as a stub.

The HttpImpl implements the functional Http interface. Let’s create a stub ...