Automation, repeatability, "atomization" and integration in the build and packaging process.
If you are going to do a program, try to work it out and forget about it (for example, a practice for a teacher), JUnit does not give you much. It is a simple program with few specifications; You look that he meets them, you deliver the program and something else.
But in the real life of a professional programmer, you write a code that is part of a complex system and after a few months or many years you have to modify it, and if you modify it you have to prove that no bugs have been introduced that make you lose money to your company, and here JUnit gives you:
-
Automation: The tests are executed automatically, you do not have to do them by hand checking results from screen (that apart from being slow, it can be misleading).
-
Repeatability: The same tests are performed each time. In addition, you do about the final version of the class. If you do the test by printing values by console and then delete those messages from your program, you are making a change in your program that you will not check with your test.
-
Atomization: Each small module (ideally each class) is tested separately. This allows us to test more thoroughly all the details of the operation of the class. If you have a complete system, testing the operation of all possible cases is much more difficult or even impossible. Also, when you enter a bug in a class, ideally you will detect it in the test of that class, which is usually easier to fix.
-
Integration: Tests are usually part of the project's build and packaging process; If they fail, the process is stopped so that the bugs have to be corrected before continuing. They are not a step that a rushed programmer can forget or do too quickly.
-
They go with the code: They are not a document of manual tests that still have not been updated.
It also has drawbacks:
-
You have to write the tests and, if the functionality of the class changes, update them.
-
They are not a panacea. The tests will test what you tell him to try. If some combination is not tested and when making a change that combination causes a bug, it will not be detected.
-
Focuses on unit tests . In theory it is to test the operation of a small module (typically a class) isolated from the others. That makes the tests simpler, but it means that you then have to test the overall performance ("by hand" or with other automated test environments oriented to system tests).
- In this case, the advantage of JUnit is that it allows you to test each class more thoroughly at the beginning of the build process; a system test is much slower and it is often not easy to modify the system to simulate special conditions.