New Feature of JUnit: Rules

card board box
card board box

I am always surprised how many unknown feature hide in a supposedly simple library. Todays example is JUnit. When inspecting the newest version (4.7) I noted an annotation I hadn’t noticed before: @Rule. WTF? I am looking at a testing framework and not at a rules engine, am I? So naturally I tried to find out what it is good for, and since not to much documentation is available my little research resulted in this blog post. Enjoy.

The purpose of the @Rule annotation is to mark public fields of a test class. These fields  must be of type MethodRule, or an implementing class. Such MethodRules behave similar to a AOP aspects, of course without use of any AOP library and specialized for Tests. They can execute code before, after or instead of a test method.  Example use cases listed in the release notes include:

  • Notification on tests
  • Setting up or tearing down resources, especially when they are used in multiple test classes
  • Special checks performed after every test, possibly causing a test to fail.
  • Making information about the test available inside the test

But ready made examples are boring, so I decided to try to implement something I was looking for all the time.

When writing acceptance tests with Fit or a similiar framework, you might end up with tons of failing tests, simply because the features aren’t implemented yet. But I don’t want this red lights hide a real red light, i.e. a test that worked before, but fails now. Therefore I’d like tests to get ignored just as if they where accordingly annotated, until they succeed for the first time. After that they should run (and possibly fail) just as a normal.

Let’s get started. First we need a little test case that can fail or succeed as we want. And this test case of course needs our annotated field:

public class DummyTest {
  1.  
  2.     @Rule
  3.     public IgnoreLeadingFailure ilf = new IgnoreLeadingFailure();
  4.  
  5.     @Test
  6.     public void testTest() {
  7.         assertTrue(false);
  8.     }
  9. }

Please note that I stripped all package and import statements for brevity. As you can see I already named my MethodRule implementation IgnoreLeadingFailure. It needs some way to track which tests ran successfully at least once. Since I am lazy, I stored this information in a property file.

public class IgnoreLeadingFailure implements MethodRule {
  1.  
  2.     private final static String PROPERTY_FILE_NAME = "activatedTests.properties";
  3.  
  4.     private final static Properties activatedTests = new Properties();
  5.  
  6.     static {
  7.         try {
  8.             activatedTests.load(new FileInputStream(PROPERTY_FILE_NAME));
  9.         } catch (IOException e) {
  10.             // actually this is to be expected on the first run
  11.             System.out.println("Couldn't load Properties from file" + e);
  12.         }
  13.     }
  14.  
  15.     public Statement apply(final Statement base, final FrameworkMethod method,
  16.             final Object target) {
  17.  
  18.         if (activatedTests.containsKey(getFullTestMethodName(method, target))) {
  19.             return base;
  20.         } else {
  21.  
  22.             return new Statement() {
  23.  
  24.                 @Override
  25.                 public void evaluate() throws Throwable {
  26.                     try {
  27.                         base.evaluate();
  28.                         activateTest(getFullTestMethodName(method, target));
  29.                     } catch (Throwable t) {
  30.                         throw new AssumptionViolatedException(
  31.                                 "This test never succeeded before, and failed again with: "
  32.                                         + t.toString());
  33.                     }
  34.                 }
  35.  
  36.                 private void activateTest(String fullTestMethodName) {
  37.                     activatedTests.put(fullTestMethodName,
  38.                             new SimpleDateFormat().format(new Date()));
  39.                     try {
  40.                         activatedTests.store(new FileOutputStream(
  41.                                 PROPERTY_FILE_NAME),
  42.                                 "tests that ran successfully at least once");
  43.                     } catch (IOException io) {
  44.                         System.out.println("failed to store properties" + io);
  45.                     }
  46.  
  47.                 }
  48.             };
  49.         }
  50.     }
  51.  
  52.     private String getFullTestMethodName(final FrameworkMethod method,
  53.             Object target) {
  54.         return target.getClass().getName() + " " + method.getName();
  55.     }
  56. }

The interesting part is the single method contained in the MethodRule interface:

public Statement apply(final Statement base, final FrameworkMethod method, final Object target)

base is the object that encapsulates the execution of the test method. The purpose of apply(..) is to provide a Statement. This Statement will get executed (or in the lingo of the interface ‘evaluated’). Typically you’ll return a Statement implementation, that wraps the Statement instance passed as a parameter.

method is the test method that will eventually get called and

target is the object containing that method. Note that you normally do not execute the method, but use it mainly to get more information about the test case the MethodRule is applied to.

With this information it should be easy to understand what my little class does: It checks if the test at hand was already executed successfully before. If this is the case, the same Statement is returned, that was passed as a parameter. Otherwise an anonymous implementation is returned which replaces any failure by a AssumptionViolatedException, and saves a successful test run in the property file. Note that the AssumptionViolatedException actually does not result in a ignored test, but gets displayed as a successful test, at least with the default runner, which doesn’t really fit my needs, but one could easily fix that by a minor change in the runner but not without that change.

So here is my opinion about this new JUnit feature: It certainly can come in handy for (integration) tests, where one tends to need a lot of resources that need quite some setup and/or tear down. Implementation of a MethodRule is fairly easy, although not exactly straight forward. The biggest problem is that it is next to impossible for an average java developer who doesn’t know this feature to understand what is going on. The annotated field is easily missed, since in many cases it isn’t used at all in the test class. Once more all this could be straight forward when java would support natively things like aspect oriented programming or open classes.

If you want more information I suggest you drop by this blog post about ‘Interceptors’ which was the name for rules at the time of writing of that post.

So, what do you think of this feature? Any ideas for features that could be implemented using rules?

Share:
  • DZone
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Twitter