Home > Articles > Programming > Java

Junit: Test-Driven Development

Jasmit Kochhar
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Programming for the Java™ Virtual Machine

Like this article? We recommend
Programming for the Java™ Virtual Machine

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.

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.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Emily NaveCommunity Tips: Starting a User Group Library
By Emily Nave on August 3, 20102 Comments

The Central Penn Adobe User Group (CPAUG) uses a library program to share books from different publishers with members. A short Q&A with group leader Megan Fister provides some great tips for starting your own.

Keep going with GWT
By Federico Kereki on August 1, 2010 No Comments

I've been using GWT for some years now, and I'm still contented with the easier way for web development. After having written a book on GWT development, doing a blog seemed a good idea for answering questions, and for further expanding topics that didn't get a place in the book.

Emily NaveUser Group Organizations: Finding Support in the Greater IT Community
By Emily Nave on July 29, 20102 Comments

Birds of a feather flock together, right? If you’re already a member of an established user group or looking for other like-minded technology evangelists, connecting with peers is an important part of being an active voice in the IT community.

See All Related Blogs

Informit Network