Here are just a few reasons to use JUnit:
JUnit tests run fast.
With JUnit, running tests is as easy and fast as running a compiler on your code. In fact, you should run your tests every time you run the compiler. The compiler tests the syntax of the code and the tests validate the integrity of the code.JUnit tests check their own results.
Testing is no fun if you have to manually compare the
expected and actual result of tests, and it slows you down.
It's also prone to error, because the human eye often misses
small details in the output. JUnit tests can be run
automatically and they check their own results.
JUnit tests give immediate feedback
When you run tests, you get simple and immediate visual
feedback as to whether the tests passed or failed. There's no
need to manually comb through a report of test results.
JUnit tests increase the stability of software.
Without tests, it's easy to become paranoid about refactoring or adding new features because you don't know what might break as a result. With a comprehensive test suite, you can quickly run the tests after changing the code and gain confidence that your changes didn't break anything ("regression testing"). If a bug is detected while running tests the bug is easily found because it was caused by the last thing you changed.
Writing JUnit tests is inexpensive.
Using the JUnit testing framework, much of the testing infrastructure is provided for you, so writing a test is as simple as writing a method that exercises the code to be tested and defining the expected result. The framework provides the context for running the test automatically and as part of a collection of other tests. This small investment in testing will continue to pay you back in time and quality.
JUnit tests increase the stability of software.
Tests validate the stability of the software and instill confidence that changes haven't caused a ripple-effect through the software. The tests form the glue of the structural integrity of the software. The more tests you write, the more stable your code becomes.
JUnit tests create a strong foundation.
JUnit tests are highly localized tests written to improve a
developer's productivity and code quality. Unlike system
tests, which treat the system as a black box and ensure that
the software works as a whole, unit tests are written to test
the fundamental building blocks of the system from the inside
out. With a firm foundation, you're more likely to have a
solid code base to build on.
JUnit tests are written in Java.
Testing Java software using Java tests forms a seamless bond between the test and the code under test. The tests become an extension to the overall software and code can be refactored from the tests into the software under test. The Java compiler helps the testing process by performing static syntax checking of the unit tests and ensuring that the software interface contracts are being obeyed.
JUnit is free and built-in to many popular IDEs!