Wednesday, March 2, 2011

JUnit using only annotations

To be honest I tried annotations with JUnit recently, but the first time I tried, it didn't go well, so I procrastinate it.
But for this "experimento", I'd to retake it with excellent results
, and this is one of test cases [I had to create a base TestCase class - you know how lazy I'm:) to avoid the process of loading my context and Log4J]
Don't forget my "golden rule", use Maven!!! I'm using JUnit 4.8
As you can see, there isn't too much to explain. Only we're using "SpringJUnit4ClassRunner.class", because to do my tests I need my Spring App Context (with all my beans)

This is a portion of my test case I haven't used much the annotations Before and After, but some day I'll find them an application.

Something really cool is the part where we can define if my test method will be Transactional, if we want to Repeat it or if after test is executed we want to Rollback the Tx.
Because, for a regular Test, just with the annotation @Test will be enough.
I felt in love with these annotations, because finally I don't have to load the a test Database and Clean it after my testing is done (I know, you still have to re initiate the sequences, but who cares, they're UNIQUE), I can reuse my existing initial DB and play with it, and nobody will see my garbage/testing data; unless I want it
 
@Transactional  --> There is a Tx involved on this test
@Rollback  --> When is done (rollback Tx)
@Repeat(2)  --> Repeat 2 Times this execution
That's what I'm talking about!!!
¡JUnit Rules!

No comments:

Post a Comment