Simplifying Testing Using a Mock Tool

We consider transforming our smart stub into a mock as the next step. To do so would involve:

  • Specifying in the test which parameters we expected (as opposed to within the stub itself)

  • Trapping and storing the parameters passed to the get() method

  • Supporting the ability to verify upon test completion that the stored parameters to get() contain the expected parameters

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

And if we created more mock implementations for other troublesome dependencies, we’d find a way to refactor ...

Get Pragmatic Unit Testing in Java 8 with JUnit now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.