Unit testing examples that proves that unit tests are worth writing [closed]
I have read a lot about unit testing, but I still haven't found any good examples that would show why unit testing is worth the time.
I would like to see actual Java code that shows how unit test is done and how it will catch possible bugs.
Unit testing is not about catching possible bugs, its about 'feeling a little bit safe'. Good example is a project with code base larger than hello world with multiple developers. It is only a matter of time, when somebody breaks something, what was working before. The question is - when will you know it? After the application is deployed or during development?
Apart from the benefits that are described by other commenters, I think that unit testing, and automated unit testing, have a positive effective on the design of your production code. Designing code that is testable tends to lead you to a much more loosely coupled design, the benefits of which are widely documented.
I have an example of when I wish I had had unit tests.
We used the decorator pattern to convert an IBitmapStream from one format into another using a number of BitmapConverter classes. A BitmapConvertor class is itself an IBitmapStream:
We had a number of bitmap converters, to convert bit depth, color spaces transformations(grey, RGB, CMYK), row padding, color component ordering, special effects, ...
We had no automated testing. So when we fixed bugs in any single converter, it required a lot of manual discipline and effort to test the converters through our GUI with a number of different test bitmaps.
The bugs were often subtle - they first became apparent when we tested a number of converters chained together - and a disadvantage of the decorator pattern is the difficulty in debugging. And fixing one bug often led to breaking another case.
It would have saved an enormous amount of time and stress if we had had an automated set of tests.
For each bug found by our QM department (or customer) we in development would have first written a unit test to replicate the bug, and then fix the code, and make sure the fix has not broken any other tests.
I can't just give an example without some elucidation. Writing unit tests is time consuming. Therefore, it is difficult to convince managers and colleagues about their being worthwhile. I write unit tests during the coding phase of development, and it can extend coding duration by 50 - 100%.
Is it worth it? I think so. Unit tests locate bugs before integration with other code modules. Also, if they check most of the run scenarios properly, they are very effective in locating broken code after development is over (maintenance).
Here's a simple example how to use JUnit 4.x:
public class UnitTests {
@org.junit.Test
public void test() {
A a = new A();
String s = " Hello ";
String result = a.trim(s);
assertEquals("The String wasn't trimmed", "Hello", result);
}
}
public class A {
public String trim(String s) {
return s.trim();
}
}
Since String.trim() removes both leading and trailing whitespace, and since the string s contains both leading and trailing whitespace, if someone later changes the method A.trim() to do something else, for example to trim only leading or trailing whitespace, this test will fail.
精彩评论