3.3. Isolating the class under test

JUnit doesn’t support mocking (faking external object communication) out of the box. Therefore, I usually employ Mockito[10] when I need to fake objects in my JUnit tests.

10

Many mock frameworks are available for Java, but Mockito is the easiest and most logical in my opinion. Some of its ideas have also found their way into Spock itself. See https://github.com/mockito/mockito.

If you’ve never used mocking in your unit tests, fear not, because this book covers both theory and practice (with Spock). I strongly believe that mocking is one of the pillars of well-written unit tests and I’m always puzzled when I see developers who neglect or loathe mocks and stubs.

The literature on mocking hasn’t reached ...

Get Java Testing with Spock now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.