在 Java 8 上为 Maven 单元测试设置时区

2022-01-16 00:00:00 timezone java-8 java maven surefire

如何在 Java 8 上的 maven Surefire 中设置单元测试的时区?

How do I set the timezone for unit tests in maven surefire on Java 8?

在 Java 7 中,这曾经与 systemPropertyVariables 一起工作,就像在以下配置中一样,但在 Java 8 中,测试只使用系统时区.

With Java 7 this used to work with systemPropertyVariables like in the following configuration, but with Java 8 the tests just use the system timezone.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <systemPropertyVariables>
      <user.timezone>UTC</user.timezone>
    </systemPropertyVariables>

为什么会这样,我该如何解决?

Why is that, and how do I fix it?

推荐答案

简答

Java 现在更早地读取 user.timezone,然后再确定 systemPropertyVariables 中的属性.解决方法是提前设置,使用 argLine:

Short answer

Java now reads user.timezone earlier, before surefire sets the properties in systemPropertyVariables. The solution is to set it earlier, using argLine:

<plugin>
  ...
  <configuration>
    <argLine>-Duser.timezone=UTC</argLine>

长答案

Java 初始化默认时区,将 user.timezone 考虑到它需要它的 first 时间,然后将其缓存在 java.util.TimeZone.现在在读取 jar 文件时已经发生了这种情况:ZipFile.getZipEntry 现在调用 ZipUtils.dosToJavaTime,它创建了一个初始化默认时区的 Date 实例.这不是一个万无一失的特定问题.有人将其称为 JDK7 中的 bug.该程序用于打印 UTC 时间,但现在使用系统时区:

Long answer

Java initializes the default timezone, taking user.timezone into account the first time it needs it and then caches it in java.util.TimeZone. That now happens already when reading a jar file: ZipFile.getZipEntry now calls ZipUtils.dosToJavaTime which creates a Date instance that initializes the default timezone. This is not a surefire-specific problem. Some call it a bug in JDK7. This program used to print the time in UTC, but now uses the system timezone:

import java.util.*;

class TimeZoneTest {
  public static void main(String[] args) {
    System.setProperty("user.timezone", "UTC");
    System.out.println(new Date());
  }
}

一般来说,解决方法是在命令行指定时区,比如java -Duser.timezone=UTC TimeZoneTest,或者用TimeZone.setDefault(TimeZone.getTimeZone编程设置("UTC"));.

In general, the solution is to specify the timezone on the command line, like java -Duser.timezone=UTC TimeZoneTest, or set it programmatically with TimeZone.setDefault(TimeZone.getTimeZone("UTC"));.

Full'ish 示例:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        ... could specify version, other settings if desired ...
        <configuration>
          <argLine>-Duser.timezone=UTC</argLine>
        </configuration>
      </plugin>
    </plugins>
  </build>

相关文章