Writing unit tests is easy in theory but could be more difficult in practice. Usually experience helps in getting better at unit testing. In this blog post, Patroklos Papapetrou shares some of his experience in writing Java unit tests.
The post is based on a code example and its initial corresponding unit tests. Patroklos Papapetrou explains some of the mistakes contained in these initial unit testing code and provides some hints to improve them:
* Developers tend to write unit tests only for the successful paths.
* The test method should be named in such a way that explains to the reader what is going to be tested and under which conditions.
* Each test method should assert one and only one thing
* You should try to be as much as specific you can when you assert the return value of a method
* You should not only invoke the method under test but you should always assert the expected result, even if it’s just a Boolean value.
* Make sure that your test will have the same behavior even if it runs from the dumpiest terminal that has access only to the code and the JDK.
Read the complete blog post on http://softwaregarden.io/common-mistakes-junior-developers-writing-unit-tests/