I have been two months on a one year old java/J2EE project, and every time the existing JUnit tests failed because of me, it was because the JUnit tests were outdated or wrong… So I am wondering “what’s the use of those tests if the only time they fail is because the name of a method has changed, or because they were done wrong”…
So… To what extent should we implement test cases, for what kind of methods? At what moment in the project and why? To sum up : any good practice to tell me about test cases ?
Thanks!
you are asking ten questions, which can fill up a major book.
I try to answer some:
what
Unit test are mainly for module tests, although they can be used for integration tests, too, the integration scope is secundary.
when
The inventor of unit test, introduced the term “test first” (write the test case in advance), because the sw developpers he knows were mostly lazy and undisciplined. If he would have recommended to test later, many test would never have been written.
My approach is, test after you completed 30%, otherwise you dont have a useable structure to test. I call that “Test early approach”
Usefull for
Junit test are less usefull for User Interface tests, that will not work easily.
Unit test are a bit difficult to write for modules that have a high integrational character, (central managing methods)
For ther rest they are quite usefull. Especially they are obligate for algorithms mathematical formulas, all kind of calulations.
One of the main advantages is that by writing unit test, you avoid time consuming debugging in real life code. The time you lose writing the unit test, that time you gain by avoiding that debugging.
Up to a unit test coverage of 80% of the source code statements, Unit test do not create additional project costs.
My experience showd, that even getter and setter should be unit tested, because that can be done in 2 minutes per class (uisng on testGetterSetter()). Last year whe had two serious bugs in setter and getter.
If you use eclipse refactor->rename, then the Unit case will be included in refactoring, as long as they are in the same project, which is recommendable.
Of course can a chnage in source code cause the neccesity to adapt the unit case, src and unit test form together an unit.
Finally one of the main advantages of unit test is, that when you create a new release that contain some changes, you still know that all your unit test run. This is especially importnt when you inherit work form colleagues, that left the company.