| By Joe Winchester | Article Rating: |
|
| February 17, 2009 07:00 AM EST | Reads: |
1,066 |
The software industry is often obsessed with progress be it in the form of a new language, wire protocol, specification update, or some other technology-driven feature. For me, software is a means to an end, and progress should be measured in features that allow code to be written more efficiently, with fewer errors, and with an increased fitness of purpose. To this end, the single thing that has most changed the way I view and approach Java is the JUnit framework.
I first came across JUnit working on a project where I was designing an API specification for a developer eight time zones away. Rather than having documents flying back and forth, he convinced me that I should write JUnit tests for the API. None of them worked. In fact to begin with none of them even compiled cleanly because the code didn't exist; however, the concept worked perfectly. The contract between the specification for what the
code would do and all the assertions for how it should behave were all defined in a single place that was alive and coupled to the code. Whenever anyone new came along and wanted to know how the class library was supposed to work, we just said "check out the JUnits." No need to have spec-ware documents that would get out of step and become inevitably dated. With the unit tests representing the specification for how the code should work as part of the same code repository as the program source, the two could be refactored together, and in a short time I became totally hooked.
On a subsequent project we created a rule - before any developer checked code into the repository, he had to run all of the JUnit tests in the workspace. Initially people squealed and complained about lack of productivity and so forth; however, it proved to be a cornerstone of the project's eventual success. I remember one bleak day when some code I'd changed had, unbeknownst to me, broken another part of the system. The developer of the piece I'd broken was furious, not at me (although he was a little peeved), but more at himself for not having written a unit test that would have shown the breakage. His view is that unit tests are an insurance policy that a piece of code you write does what it says it is going to (using assert statements and other JUnit test constructs), but more important that by making your test part of the larger test suite, each time a developer makes a change, she can see the breakage and either fix the code, or fix the test if it's no longer applicable. Another thing we adopted on the project is that whenever a defect was raised, the first task was to write a failing test. This could take the better part of a day, sometimes longer, even if the fix was only a 10-minute hack. The benefit however is that once the failing test goes from red to green and both code and test alike are committed to the repository, there should be no way the problem can re-occur. The more tests we had in our suite, the better our insurance coverage.
For any project I work on now the tests form part of the build process. It's not enough to have a clean compile; you have to have a clean run of all of the tests for a build to be blessed as "complete" from development and taken to the next stage of the production process. This makes the functional testers happy as what they're getting is higher quality, allowing them to spend more time looking for integration or usability problems. It also lowers the entry bar for new developers as the project now has an insurance policy against sloppy regression in the form of an integral suite of unit tests.
I now view JUnit as an integral part of the Java development process, as important as compilation; one picks up syntactic compilation errors and the other safeguards runtime behavior. If you don't already use JUnit, you should, and then you too can catch the test bug.
Published February 17, 2009 Reads 1,066
Copyright © 2009 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Joe Winchester
Joe Winchester, Editor-in-Chief of Java Developer's Journal, was formerly JDJ's longtime Desktop Technologies Editor and is a software developer working on development tools for IBM in Hursley, UK.
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Journal Opens "Readers' Choice Awards" Nominations
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Industry Experts Discuss the State of Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- It's the Java vs. C++ Shootout Revisited!
- The End of IT 1.0 As We Know It Has Begun
- An Introduction to Abbot
- Java Kicks Ruby on Rails in the Butt
- Interviewing Java Developers With Tears in My Eyes
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- 1st Annual Government IT Expo: Call for Papers Deadline July 15
- How to Diagnose Java Resource Starvation
- REA Is Where RIA Becomes the Norm
- Kindle 2 vs Nook
- Anatomy of a Java Finalizer
- Why IBM’s Server Chief Got Busted
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?


































