[hibernate-dev] Length of test methods

Gail Badner gbadner at redhat.com
Fri May 25 14:08:18 EDT 2012

----- Original Message -----
> From: "Hardy Ferentschik" <hardy at hibernate.org>
> To: "Hibernate" <hibernate-dev at lists.jboss.org>
> Sent: Friday, May 25, 2012 8:08:07 AM
> Subject: [hibernate-dev] Length of test methods
> 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 updated that test and added a lot of those assertions to do comprehensive testing.

> I am having several problems with this type of testing:
> * test method should be descriptive, for example
> testForeignKeyBindingManyToOne, testBagBinding, testSetBinding, …

Yep, true.

> * 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)

Yep, true.

> * Avoid assertTrue/False. assertEquals has a much more helpful
> message if things fail. If you use assertTrue/False add

OK, that's fine. I can add that when I revisit that test.

>   a message about your expectation
> * Add assertion messages (also for assertEquals)

Personally, I find the messages about the expectation confusing so I don't use them.

> 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.

Why did I do it this way? Mainly for expediency. To avoid having countless tiny little tests that have the same setup. Yes, it could be done better/differently. I was trying to do comprehensive testing at the EntityBinding level because, ultimately, that's the source for nearly all information that gets processed by the persisters. I wanted to make sure that everything (including domain and relational models) got integrated properly into the binding.

I'll look at breaking things up and documenting better when I revisit this test.


> --Hardy
> _______________________________________________
> hibernate-dev mailing list
> hibernate-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/hibernate-dev

More information about the hibernate-dev mailing list