If you’re having trouble testing legacy code, mock a logger

I was working on some legacy code (“legacy” meaning it has no automated test coverage) and I really wanted to write some automated acceptance tests for it.  The problem is, I could not think of a way to do that without refactoring the code first.  I did not want to do this because “it was already working” and the refactoring could have introduced new bugs.  I had a chicken and egg problem: I didn’t want to modify code until I had automated tests but I had to modify code to write the automated tests.  Which comes first?

I ended up using a trick that minimally changes the production code, actually improves the production code base, and allows me to write automated tests.  It’s these two simple steps:

Continue reading

11 things I learned about TDD by looking at the tests in the JUnit source

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated with SUnit.

JUnit was originally written by Erich Gamma and Kent Beck. The latter literally wrote the book on Test Driven Development.  I thought it would be worthwhile to look at some of the tests in the library and draw attention to some interesting things about them because it may change people’s ideas of how to practice TDD.

Continue reading

To those that advocate manual tests instead of automated tests

There are people out there who are not the biggest fans of automated tests.  They think they cause more trouble than they are worth.  I can definitely sympathize with that perspective. In fact, I will readily admit it can be completely true.  A bad automated test can be worse than no test.  But, I’ve worked on projects that had a lot of good automated tests and I think they were better off for them.

There are people out there who think writing all these automated tests is usually more trouble than it’s worth.  The alternative to automated tests is to “run your software frequently in a live scenario to test the features as they’re being deployed“.  But, I have some questions for the people that feel this way:

Continue reading

Prevent your production issues from ever happening again

This is a scenario that’s all too common: The product gets released and then a serious problem is discovered.  You think, “That couldn’t have been caused by me”, but then you realize that it was.  After you’re done beating yourself up about the mistake you should ask yourself this question next:  How can I prevent this problem from ever occurring again?

If your answer is, “be more diligent in the future” or “don’t do that”, bzzz, wrong answer.  That’s not good enough.  It allows other employees to make the same mistake in the future.  If your answer is, “update the documentation to say ‘don’t do this'”, bzzz, wrong answer.  If this mistake can take down an environment, it’s too risky to assume someone will read the documentation to avoid this in the future.  Don’t give someone the opportunity to miss this information.

Continue reading

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