Search⌘ K
AI Features

Simplifying Testing using a Mock Tool

Discover how to simplify unit testing by transforming stubs into mocks and using the Mockito framework. Learn to write concise tests with mocks that track and verify method calls, reducing redundancy and improving clarity in Java testing.

Transforming stub into mock

Let’s consider how to transform our smart stub into a mock. To do so would involve:

  • Trapping and storing the parameters passed to the get() method.
  • Supporting the ability to verify upon test completion that the stored parameters to get() contains the expect parameters.

Creating a mock that performs those steps may seem like overkill. What does it get us? Actually, not much at all. If we were to write a second or third test that used the same mock, though, we’d shrink the amount of code we need to write for each.

If we create more mock implementations for other troublesome ...