Wednesday, June 11, 2008

Test Driven Development

Test Driven Development is normally when you write tests, and then write code to make those test pass.

I find that a good way to start out with test driven development is to modify it slightly. Instead of writing the test first, write the method. Then immediately write the test. Often people new to TDD take a while to learn how to write tests and having the method under test helps with the process of learning to write tests.

Tools
The first tool that you need to learn is JUnit. JUnit allows you to quickly write tests to exercise your code, espically JUnit 4 introduced annotation based tests, which makes writing tests very easy.

The next tool you should look at is a mock object package. I personally use jMock, but other people swear by easyMock. Mock objects let you isolate an object under test. The key to being able to use mock objects is to decouple your dependencies.

I personally prefer using dependency injection to decouple your dependenices. Martin Fowler has a good overview of the subject on his website.

Time
How much of your development time should be spent writting unit tests? In general, I find that a good rule of thumb is that about 50% of your development time should be on unit tests and 50% on production code.

Advantages
TDD tends to produce better production code. In order to make the production code testable, you wind up writing code with a lower cyclomatic complexity, thus tending to have fewer bugs in your code to begin with.
TDD gives you good unit test coverage. You should have test for each method, most likely you'll have more than one test for each method, ideally you should have at least the cyclomatic complexity number of tests.
Due to the good test coverage, you can refactor with confidence that you will detect very quickly if you do break code.

Give it a try, it may take a while to get used to, but once you do, you'll be happy you did.

Monday, June 9, 2008

Sunday, December 16, 2007

Agile Development

I've been using Agile techniques for years and trying to get others to use them as well. I've read books on Agile techniques, read blogs, and written code. For people interested in exploring these techiques, I would suggest the following techniques to try out:
  1. TDD - Test Driven Development
    • Write unit tests before or as soon as you write a new class or add methods
    • Design for easy testing, limit and break dependencies
    • Make sure all your test past before moving on to develop more code
    • Use a testing framework like JUnit.
    • Use mock objects. A mock object framework like jMock or EasyMock will help.
  2. Refactoring
    • Use an IDE which supports refactoring, such as Eclipse.
    • Read Martin Fowler's book and blog.
    • Refactor frequently, don't be afraid of refactoring. If you have good set of unit tests they will quickly pickup any mistakes that might be introduced by refactoring.
  3. Break Dependencies
    • By breaking dependencies using a technique like dependency injection, you can make writing Unit tests easier and make refactoring safer.
    • During testing, inject by hand, during production use a framework such as Spring or Guice.
This should get you started, I'll add more techniques and go into depth on each technique during future posts.

What is this blog about

In my role as an internal Java Framework developer for a corporation developing web applications, I realized that many developers don't understand how to use the modern tools and techniques available to them. This blog is about presenting those tools and technique so that others can benefit from this experience.