Do you really hate unit tests or do you just hate certain kinds of unit tests?

There are a lot of people out there who will say, “I hate unit tests” or “unit tests are usually not worth the hassle” but when you dig down into what people really mean, they’re actually saying, “I hate this specific kind of unit test.”  The problem is most people think there is only one kind and that everyone agrees on their definition.  This actually isn’t true and I’m going to explain all the varieties in this article.

A lot of people have a very specific idea of what a unit test is and think any variation on that is not a “true” unit test.  They’ll say things like, “What you’re calling a unit test isn’t really a unit test because you’re doing x, y and z” or,  “This isn’t a unit test, it’s an integration test”.  Sometimes that’s true, but a lot of times it’s not.  As you’ll see later, the boundary between unit test and integration test is not so clear cut.

You may feel like you dislike unit tests, but you shouldn’t be so hasty about your opinion if you don’t know about all the different types.  There may be a variety out there that you really enjoy or you are already doing without considering them to be unit tests.

Continue reading

How to mock out a deeply nested class in Spring without going insane

Lets say you have a bunch of classes that call each other like this:

Here, Root  calls Middle  which calls End  which calls Database .  If you would like to test from Root , and want to mock out the Database how can you do that in Spring?   Spring doesn’t make this easy. But I have figured out how to do this in a way that I consider satisfactory.

Continue reading