x
Loading
 Loading
Hello, Guest | Login | Register

Putting Your Code to a Test

For something that’s so simple its creators say that you can “write your own equivalent,” the JUnit testing framework has nonetheless become an indispensable part of the development process for many Java coders.

For something that’s so simple its creators say that you can “write your own equivalent,” the JUnit testing framework has nonetheless become an indispensable part of the development process for many Java coders.

JUnit is a Java class library that supports unit testing, or testing that compares the actual result of a discrete method with an expected result. If the actual result and the expected result match, the test passes; otherwise, it fails.

The idea behind unit testing is that you should create one or more unit tests for a class method when — or even better, before — the method is written. Each unit test codifies correctness, and a class isn’t released until it passes 100 percent of its unit tests. Later, when you return to the code and make changes for a new release, a failure in a unit test indicates a flaw or a regression (where previously working code now behaves differently and incorrectly) in some of the newly-introduced code.

Unit testing provides concrete evidence that a snippet of code is functioning properly. Used consistently, unit testing lets developers introduce new features into working code or refactor existing code with confidence. In some sense, using JUnit for “test-driven development” is like doing pair programming with a partner that has a photographic memory of every weird use case that’s been overcome during the course of a project. Not only does this impossibly perfect collaborator remember all past obstacles, he thumps you on…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace