Mocking

It’s very hard, if not impossible, to unit-test a large piece of code that has dependencies. (What’s large? Any code we can’t see entirely without scrolling down in an editor—no, don’t make the font size smaller now.) One advantage of unit testing is that it forces us to make the unit of code smaller. Smaller code is cohesive code. It also forces us to decouple the code from its surroundings. This means less coupling. Higher cohesion and lower coupling are qualities of good design. In this section we’ll discuss ways to deal with dependencies; ways to unit-test code with dependencies are covered in the rest of this chapter.

Coupling comes in two forms: code that depends on our code, and code that our code depends on. We need to address ...

Get Programming Groovy 2 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.