Saturday, October 6, 2012

Junit @Rule

Recently, I've found an article on Junit about annotation @Rule.I didn’t know this annotation before.I will present you briefly how it works and I will give you some interesting links.

In earlier version of Junit, we need to declare @SetUp and @TearDown for each test class.
Now, with newer version of Junit, it’s possible to define its own rule with @Rule and you will have the possibility to execute code during test (Same thing than AOP).
To help you to understand, here is some standard rules :

  • ExpectedException: check if an exception is thrown during test
  • Timeout: check that one test doesn’t exceed a given time
  • ExternalResource: Open or close a resource.
  • TemporaryFolder: create and destroy a temporary directory.

Ref :