JUnit is an open source, regression-testing framework that lets Java developers write unit tests as they develop systems. Jasmit Kochhar shows you how to use it to establish a close relationship between testing and development.

Like this article? We recommend

Like this article? We recommend

Have you ever worked on a projectfor which you wrote thousands of lines
of codeand then needed to make an enhancement to one of the base classes
that you wrote at the start of the project? What would break if you made the
change? Is there any way to do so and yet be assured that the system can be
exhaustively checked for any problems? How do you know whether the code given to
you works as advertised? Do you just take the developer's word for it or
can you do better than that? What if you're trying to integrate code that
someone else wroteand you no longer have access to that person?

Wouldn't it make sense to have a framework that enables you to develop
and test your code as you go through the development process and be assured that
the individual tests so developed will work when you have the entire system up
and running? This is the objective of using a unit test-based framework.

This article discusses the JUnit framework and suggests ways to effectively
use it.

What Is JUnit?

JUnit is an open source, regression-testing framework for Java that
developers can use to write unit tests as they develop systems. The framework
helps to establish a close relationship between testing and development. You
first write down the way you intend your code to work. Then, as you write the
code, you use the JUnit test runners to verify how much it deviates from the
intended goal. Integration testing confirms that the different subsystems work
well when they are put together. Acceptance testing simply confirms that an
application does what the customer expects it to do. Unit tests are so named
because they test a single unit of code, which can be a single class for
Java.

Unlike the typical unit tests, in which you may be inclined to write after
you complete a module, JUnit encourages a cycle of coding and testing during the
development cycle. Hence, the focus is less on module level functional tests and
more on testing the fundamental building blocks of a system one block at a time.
This leads to the development of comprehensive test suites that you can run any
time after changing your code and be confident that the refactoring or code
modification will not break any of the subsystems without your knowledge.