Digital Edition

SYS-CON.TV
Catching the Test Bug
If you don't already use JUnit, you should

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.

About 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.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1



ADS BY GOOGLE
Subscribe to the World's Most Powerful Newsletters

ADS BY GOOGLE

SYS-CON Events announced today that DatacenterDynamics has been named “Media Sponsor” of SYS-CON's 1...
CloudEXPO New York 2018, colocated with DXWorldEXPO New York 2018 will be held November 11-13, 2018,...
Dynatrace is an application performance management software company with products for the informatio...
@DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22...
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, ...
Today, we have more data to manage than ever. We also have better algorithms that help us access our...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: D...
A valuable conference experience generates new contacts, sales leads, potential strategic partners a...
DXWorldEXPO LLC announced today that ICOHOLDER named "Media Sponsor" of Miami Blockchain Event by Fi...
SYS-CON Events announced today that IoT Global Network has been named “Media Sponsor” of SYS-CON's @...
The best way to leverage your Cloud Expo presence as a sponsor and exhibitor is to plan your news an...
DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novemb...
DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing w...
Disruption, Innovation, Artificial Intelligence and Machine Learning, Leadership and Management hear...
Enterprises are striving to become digital businesses for differentiated innovation and customer-cen...
DXWorldEXPO LLC announced today that Telecom Reseller has been named "Media Sponsor" of CloudEXPO | ...
Consumer-driven contracts are an essential part of a mature microservice testing portfolio enabling ...
"Calligo is a cloud service provider with data privacy at the heart of what we do. We are a typical ...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As au...
DXWorldEXPO LLC announced today that "IoT Now" was named media sponsor of CloudEXPO | DXWorldEXPO 20...