A JUnit Rule to Ease SWT Test Setup

Home  >>  Eclipse  >>  A JUnit Rule to Ease SWT Test Setup

A JUnit Rule to Ease SWT Test Setup

On February 25, 2014, Posted by , In Eclipse,JUnit, By ,,,,, , With No Comments

Rüdiger and I have written a lot of SWT test cases for our current RCP project lately. Most of the time we can execute them as plain JUnit tests. Still they also have to run in a RCP integration environment. Long story short we wrote a DisplayHelper fixture to reduce redundancy and handle display related stuff for both cases properly. With stuff I mean disposal of open shells, flushing of pending events and a few other goodies.

 Testing with JUnit

Testing with JUnit Book

Testing with JUnit is one of the most valuable skills a Java developer can learn. No matter what your specific background, whether you’re simply interested in building up a safety net to reduce regressions of your desktop application or in improving your server-side reliability based on robust and reusable components, unit testing is the way to go.

Frank has written a book that gives a profound entry point in the essentials of testing with JUnit and prepares you for test-related daily work challenges.

Learn more...

If you are interested, the DisplayHelper is available as a GitHub gist. The download also contains a little demo that shows what the helper can do:

https://gist.github.com/fappel/9207164

As an interesting side note I’d like to mention that the helper wasn’t a rule right from the start. It was used like this (which is still possible for particular use cases):

public class FooTest {
  
  private DisplayHelper displayHelper;

  @Before
  public void setUp() {
    displayHelper = new DisplayHelper();
  }
  
  @After
  public void tearDown() {
    displayHelper.dispose();
  }
  
  @Test
  public void testFoo() {
    // now you can do the important stuff
  }
}

But then Holger‘s post about his simple Tabris/RAP Test Runner rang a bell. I did not strive for a test runner, but why not using an JUnit TestRule? Well, this should probably have been obvious for someone who has written a post called JUnit Rules… Anyway, it workes like a charm! Now the same test case can be written as:

public class FooTest {
  
  @Rule
  public DisplayHelper displayHelper = new DisplayHelper();
  
  @Test
  public void testFoo() {
    // now you can do the important stuff even faster...
  }
}

Less code, less typing, less chances to do something wrong – such things make me happy 🙂

And to let you participate with my happiness I also created little Eclipse java template to generate JUnit 4 Rules:

rule-selection

It is available for download in my JUnit 4 templates gist: https://gist.github.com/fappel/8863732

Hope you like it 😉

It would be remiss not to tell you that chapter 6, Reducing Boilerplate with JUnit Rules, of my book Testing with JUnit is available as a free reading sample at https://www.packtpub.com/packtlib/book/Application%20Development/9781782166603/6. So, in case you're not tired of my scribblings yet, boldly go ahead and take the opportunity to delve deeper into the world of JUnit rules...
Follow me

Frank Appel

Frank is a seasoned programmer, team leader, trainer, author and presenter with two decades of experience in professional software development.

Specialized in the Java language and focused on quality-oriented agile paradigms he is a stalwart of test-driven practices in particular.

He understands the creation process of software as a craftsmanship based on a well-balanced mix of knowledge and the experience of the daily work.

fappel@codeaffine.com
Follow me

Leave a Reply

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