Setting timezone for maven unit tests on Java 8

后端 未结 1 2004
青春惊慌失措
青春惊慌失措 2020-12-05 04:52

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

With Java 7 this used to work with systemPropertyVariables like in the following c

相关标签:
1条回答
  • 2020-12-05 05:14

    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>
    

    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());
      }
    }
    

    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 example:

      <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>
    
    0 讨论(0)
提交回复
热议问题