About Keyhole Software

Keyhole is a midwest-based consulting firm with a tight-knit technical team. We work primarily with Java, JavaScript and .NET technologies, specializing in application development. We love the challenge that comes in consulting and blog often regarding some of the technical situations and technologies we face.

The Importance of Unit Testing

I’m currently working on an enhancement to an existing insurance premium rating algorithm. Anyone who has done work in insurance (whether it be in IT or on the business side) is probably well aware of how complex this can become.

Consequently, any significant enhancement to such a system is quite likely to be fraught with pitfalls and setbacks, and that’s even before considering the level and quality of business requirements and testing strategies. However, the existence of unit tests can help to minimize these risks.

Both unit tests that exist before development enhancements and unit tests you create as part of the development effort can be beneficial.

Existing unit tests benefit developers in at least two ways:

  1. They could be considered a form of requirements as to how the system currently operates. Unit tests provide concrete expressions of business requirements. For those who understand concepts better through examples (such as me), these existing tests can be invaluable. Even with well-written business requirements, sometimes understanding can only come through examples such as this. It also provides the developer with a debugging opportunity for deeper understanding of the existing code, should this be found to be necessary.
  2. They help to ensure that changes made by the enhancement do not break the existing system, or gives the developer a testing pathway, should the existing behavior need to be changed due to the enhancement.

New tests also provide their own benefits:

  1. It should be obvious that they help to ensure that code meets requirements.
  2. They also help to ensure that all logic conditions are accounted for. They force the developer to revisit the code they have just written and to approach it from different directions. This helps to root out conditions the developer might not otherwise think of, resulting in them to then alter the code to properly handle these conditions.

Sufficient Coverage

So, what are some ways we can ensure that we write unit tests with sufficient coverage? Below is a short list of some guidelines to follow:

  1. How many tests are enough? Nothing that can break should be left untested, even seemingly trivial ones.
  2. Most modern development environments have tools that will expose code that does not get executed during the run of your unit test suite. Use these tools to ensure you get the most thorough test coverage you can.
  3.  Test boundary conditions, especially when testing something as complex as the algorithm I referred to in my introduction.
  4. Bugs tend to collect within the vicinity of each other. If one bug is found, it is good practice to thoroughly test the code around it, since it is likely other bugs will exist there as well.
  5. Lastly, and this can sometimes be difficult, tests must be as fast as you can possibly make them. When time gets short, it will be the longest running tests that get sacrificed first. As an example, I’ve had unit tests in the past that have processed over xml document provided as the result of a service call. These documents were huge, containing the data for entire policies, most of which we never even needed. To cut down on processing time, we edited these documents to contain only the data relevant to the tests being run.

Unfortunately, the decision to create unit tests or not isn’t always up to the developer. As I previously stated, I think most of those involved understand the importance on a theoretical level, but just are not willing to sacrifice the time up front to ensure that unit tests are written. However, as developers, we must try to incorporate them when we can.

Reference: The Importance of Unit Testing from our JCG partner Robert Rice at the Keyhole Software blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


+ 3 = eleven



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close