Can you explain in a few sentences:
- Why we need it / why they make our life easier ?
- How to unit-test [simple example in Java] ?
- When do not we need them / types of projects we can leave unit-testing out?
- useful links
Can you explain in a few sentences:
unit tests are code snippets that test your classes/methods to make sure they work as expected. theyre useful if written before implementing the actual classes/methods and/or as regression testing - to make sure you dont break any existing functionality when you touch it later.
the easiest way to see a working example is to install maven, run "mvn archetype:generate" and select 15 (the simple hello world project) when prompted for archetype id. that will generate a simple working project with one class and one test case class
Because that is your proof that the application actually works as intended. You'll also find regression bugs much easier. Testing becomes easier, as you don't have to manually go through every possible application state. And finally, you'll most likely find bugs you didn't even know existed even though you manually tested your code.
Google for junit
Unit tests should always be written, as said, it is your proof that the application works as intended. Some things cannot or can be hard to test, for example a graphical user interface. This doesn't mean that the GUI shouldn't be tested, it only means you should use other tools for it.
See point 2.
It's probably work reading the Wikipedia article on Unit Testing, as this will answer most of your questions regarding why. The JUnit web site has resources for writing a Java unit test, of which the Junit Cookbook should probably be your first stop.
Personally, I write unit tests to test the contract of a method, i.e. the documentation for a particular function. This way you will enter into a cycle of increasing your test coverage and improving documentation. However, you should try to avoid testing:
JUnit is not the only unit testing framework available for Java, so you should evaluate other frameworks like TestNG before diving into it.
In addition to "top-level" frameworks, you will also find quite a few projects covering specific areas, such as:
One of the best books on the hows and whys of unit testing in Java is Pragmatic Unit Testing in Java with JUnit (Andy Hunt & Dave Thomas)
What I would have written is already covered in many of the responses here but I thought I'd add this...
The best article I ever read on when to use/not to use unit tests was on Steve Sanderson's blog. That is an excellent article covering the cost/benefit of unit testing on different parts of your code-base (i.e. a compelling argument against 100% coverage)
Why we need it / why they make our life easier ?
How to unit-test [simple example in Java] ?
Check out the JUnit website and the JUnit cookbook for details. There isn't much to writing JUnit test cases. Actually coming up with good test cases is surely harder than the actual implementation.
When do not we need them / types of projects we can leave unit-testing out?
Don't try to test every method in a class, but rather focus on testing the functionality of a class. Beans for example, you won't write tests for the getters and setters...
Links
JUnit - Unit testing
EclEmma - test coverage tool
link text - Wikipedia link to unit testing
This is how how your programming should be :