Java Reference
In-Depth Information
13.2
JU NIT :W HY A LL THE F USS ?
JUnit is a framework for unit tests. It consists of a handful of classes which you
can use to build bunches of test cases for testing your application. JUnit also
comes with three test “runners” for running your tests and reporting the test
results. So why all the fuss? Why has JUnit been so much in the technical
forefront the last year or two?
Start with a straightforward idea, well executed, that can help almost any
programmer working on any application. Make it something that can be inte-
grated incrementally into existing projects. Make it robust enough to be used
for projects starting “from scratch.” Give it a simple but pleasing GUI, and put
it to work on a few high-profile projects. Give it some good press coverage. And
you've got a winner: You've got JUnit. Besides, it really does help you get useful
work done; it makes writing tests a little less work and a little more enjoyable.
And working with well-tested code is its own reward—a satisfying experience.
13.3
D ESIGN T HEN T EST T HEN C ODE
This is the slogan of the test-oriented crowd, and if it sounds a bit impossible,
it is. It's hype—it got your attention, and there is a bit of truth to it, but don't
take it too literally.
The approach espoused by the “Testing First” crowd is to start, like all
good software development efforts, with design. But once you have pieces de-
signed, move directly into testing. Now you don't have any code that can be
tested yet, but you can start writing your tests. Then—although the tests will
fail, as there is no code to run yet—you can begin keeping score on your
progress by running these tests as code gets implemented.
NOTE
Some people like to tout the use of JUnit as an automated tool to track progress,
but that's a little hard to do when you can't compile your tests because the
classes they need don't yet exist. However, if you document your design of a
class by (among other things) creating an empty version of the source, with
Javadoc comments for the class and whatever methods you have come up with
so far, well, then you've got something that will compile, and thus can be used
for tracking progress. It also makes great, tangible documentation. Our point
here, though, is that you are doing some coding before you begin testing. It's
really more of a back-and-forth between coding and testing.
Search WWH ::




Custom Search