Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit custom annotation

I wanted to create a custom JUnit annotation, something similar to expected tag in @Test, but I want to also check the annotation message.

Any hints how to do that, or maybe there is something ready?

like image 410
Szymon Lipiński Avatar asked Oct 27 '11 19:10

Szymon Lipiński


1 Answers

JUnit 4.9 tightened up the library's use of "rules" for tests, which I think might work as well as a custom annotation. Take a look at TestRule as a starting point. You can implement a rule based on that interface, and then use either the @ClassRule or (method-level) @Rule annotations to put them into play in your tests.

A good concrete example is ExpectedException, which lets you specify exceptions like the expected parameter for @Test does (and then some).

like image 76
bhavanki Avatar answered Oct 10 '22 04:10

bhavanki