Setting timezone for maven unit tests on Java 8(在 Java 8 上为 Maven 单元测试设置时区)
问题描述
如何在 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>
这篇关于在 Java 8 上为 Maven 单元测试设置时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 8 上为 Maven 单元测试设置时区
 
				
         
 
            
        基础教程推荐
- 大摇大摆的枚举 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- Java Swing计时器未清除 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
						 
						 
						 
						 
						 
				 
				 
				 
				