Agile Zone is brought to you in partnership with:

Shekhar Gulati is a Java Consultant with over 5 years experience.He is currently working with Xebia India an Agile Software Development company.The postings on this site and on his blog are his own and do not necessarily represent opinion of his employer. His own blog is at http://whyjava.wordpress.com/ and you can follow him on twitter here. Shekhar has posted 25 posts at DZone. View Full User Profile

JUnit 4.9 - Class and Suite Level Rules

01.31.2011
| 19049 views |
  • submit to reddit

If you have worked with JUnit Rule API which was introduced in version  4.8 then you might probably also think that they are very useful. For example, there is a Rule called TemporaryFolder which creates files and folder before test is executed and deletes them after the test method finishes(whether test passes or fails). For those who are not familiar with Rule API , a Rule is an alteration in how a test method, or set of methods, is run and reported.

Before version 4.9 Rule can only be applied to a test method not to a Test class or JUnit test suite. But with JUnit 4.9 which is not yet released you can use @ClassRule annotation to apply a rule to test class or a test suite. If you want to use JUnit 4.9 download it from JUnit git repository. The @ClassRule annotation can be applied to any public static field of Type org.junit.rules.TestRule. The class level rule can be applied in scenarios where you use normally use @BeforeClass and @AfterClass annotation. Some of the scenarios can be like when you want to start a server or any other external resource before a test class or test suite, or when you want to make sure that your test suite or test class runs within a specified time, etc. The advantage of using class level rule is that they can be reused among different modules and classes.

Let's take an example when we want to make sure that our test suite should run within x seconds otherwise test should timeout. As you can see below we TestSuite AllTests runs TestCase1 and TestCase2. I have defined a suite level rule which would make sure that test run in three seconds otherwise suite will fail.

Test Suite

@RunWith(Suite.class)
@SuiteClasses({ TestCase1.class, TestCase2.class })
public class AllTests {
@ClassRule
public static Timeout timeout = new Timeout(3000);
}

 TestCase 1

public class TestCase1 {
@Test
public void test1() throws Exception{
Thread.sleep(1000);
Assert.assertTrue(true);
}
}

 TestCase2

public class TestCase2 {
@Test
public void test2() throws Exception{
Thread.sleep(1000);
Assert.assertTrue(true);
}
}

This is the most important feature which will be released in version 4.9 of JUnit.

Published at DZone with permission of its author, Shekhar Gulati.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Richard Langlois replied on Thu, 2011/05/12 - 10:34am

Excellent article, keep up the good work, Richard Langlois, Principal Software Engineer, Silverlink Communications, Burlington, MA.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.