Skip to content

Support set / reset time zone and locale during test execution #4727

@strangelookingnerd

Description

@strangelookingnerd

Code under test is oftentimes influenced by current locale and time zone. This usually leads to boilerplate code such as

private Locale locale;
private TimeZone timeZone;


@BeforeEach
void setUp() {
  locale = Locale.getDefault();
  Locale.setDefault(Locale.US);

  timeZone = TimeZone.getDefault();
  TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}

@AfterEach
void tearDown() {
  Locale.setDefault(locale);
  TimeZone.setDefault(timeZone);
}

An annotation-based solution would remove the need for such boilerplate code and allow a cleaner code style and thus improve readability. The annotation should be supported on both, class and method level. Further, in order to address concurrency issues ResourceLock should be utilized to allow safe manipulation of locale and time zones when tests run in parallel.

There is an existing solution in junit-pioneer. https://junit-pioneer.org/docs/default-locale-timezone/ contains more examples for potential use cases.
That being said, while there is a 3rd-party library, I still think that this is a feature that should be included in JUnit already.

I'd be happy to prepare a pull request for this in case there is positve feedback for this feature.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions