JUnit Categories

Hey there, this is my second post on JUnit and its lesser known features. To read the first one go here.

JUnit Categories allow tagging tests thus dividing the tests into separate buckets. This, in turn, becomes important in order to choose which tests run at a later time.

I find categories in combination with maven quite useful in splitting tests into unitary and integration tests. This is probably better understood with an example.

 

Following maven config will split the tests into unitary tests and integration tests. Note how we’re using surefire for the unitary tests and failsafe for the integration tests. We could have used surefire all the way and by all means do if you only have unitary tests. However, failsafe makes sure that failing tests are torn down correctly and that the post-integration-test phase (that comes after the integration-test) is called. The surefire plugin would just stop everything during the integration-test phase. But, anyways, this is maybe an article for another time. Here’s the example:

 

In order to skip the integration tests you can add -DskipITs command line parameter to the maven invocation.

Leave a Reply

Your email address will not be published. Required fields are marked *