The Artima Developer Community
Sponsored Link

Agile Buzz Forum
JUnit tip: Setting the default timezone with a TestDecorator

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Joe Walnes

Posts: 151
Nickname: jwalnes1
Registered: Aug, 2003

Joe Walnes, "The Developers' Coach" from ThoughtWorks
JUnit tip: Setting the default timezone with a TestDecorator Posted: Jan 9, 2005 3:47 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by Joe Walnes.
Original Post: JUnit tip: Setting the default timezone with a TestDecorator
Feed Title: Joe's New Jelly
Feed URL: http://joe.truemesh.com/blog/index.rdf
Feed Description: The musings of a ThoughtWorker obsessed with Agile, XP, maintainability, Java, .NET, Ruby and OpenSource. Mmm'kay?
Latest Agile Buzz Posts
Latest Agile Buzz Posts by Joe Walnes
Latest Posts From Joe's New Jelly

Advertisement

A problem I was finding when testing XStream is that many of the tests were timezone dependent. Initially I had some code in each setUp()/tearDown() method to set the default timezone and reset it afterwards.

This lead to a lot of duplication. Putting the common code in a super class was an option but this lead to a fragile base class.

Using composition allows this commonality to be put in one case and applied to the relevant test cases. This gets out of the 'single inheritance' issue.

JUnit provides an (often overlooked) decorator class to help with this. Here's my TimeZoneTestSuite:

  public class TimeZoneTestSuite extends TestDecorator {

    private final TimeZone timeZone;
    private final TimeZone originalTimeZone;

    public TimeZoneTestSuite(String timeZone, Test test) {
      super(test);
      this.timeZone = TimeZone.getTimeZone(timeZone);
      this.originalTimeZone = TimeZone.getDefault();
    }

    public void run(TestResult testResult) {
      try {
        TimeZone.setDefault(timeZone); 
        super.run(testResult);
      } finally {
        TimeZone.setDefault(originalTimeZone); // cleanup
      }
    }

  }

To use it, you need to override the default test suite behavior by adding a method to your TestCase:

  public class MyTest extends TestCase {
    public static Test suite() {
      Test result = new TestSuite(MyTest.class); // default behavior
      result = new TimeZoneTestSuite("EST", result); // ensure it runs in EST timezone
      return result;
    }

TestDecorators are a very powerful feature of JUnit - don't forget about them.

Read: JUnit tip: Setting the default timezone with a TestDecorator

Topic: Open Source and free riding Previous Topic   Next Topic Topic: Where was that left turn?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use