[hibernate-dev] Length of test methods
Hardy Ferentschik
hardy at hibernate.org
Fri May 25 11:08:07 EDT 2012
Hi,
looking at some metamodel test code today I found a test method 'doTest' w/ 200 lines of code and countless assertions
of the form 'assertTrue/False(condition)', 'assertEquals(expected, actual)'.
I am having several problems with this type of testing:
* test method should be descriptive, for example testForeignKeyBindingManyToOne, testBagBinding, testSetBinding, …
* test method should be short and in a perfect case one make a couple of assertions per test (then you often don't even
need much comments, because the test name directly hints to what you are testing). Rather repeat some setup code
in each method than asserting EVERYTHING there is to a EntityBinding (as example)
* Avoid assertTrue/False. assertEquals has a much more helpful message if things fail. If you use assertTrue/False add
a message about your expectation
* Add assertion messages (also for assertEquals)
Why do I care? Once the code breaks you need at a first step understand what actually gets tested and that is easier with
short descriptive methods and assertion messages.
--Hardy
More information about the hibernate-dev
mailing list