Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java8上为maven单元测试设置时区_Java_Maven_Timezone_Java 8_Surefire - Fatal编程技术网

在Java8上为maven单元测试设置时区

在Java8上为maven单元测试设置时区,java,maven,timezone,java-8,surefire,Java,Maven,Timezone,Java 8,Surefire,如何在Java8上的maven surefire中设置单元测试的时区 在Java 7中,这用于处理systemPropertyVariables,如以下配置中所示,但在Java 8中,测试仅使用系统时区 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configur

如何在Java8上的maven surefire中设置单元测试的时区

在Java 7中,这用于处理
systemPropertyVariables
,如以下配置中所示,但在Java 8中,测试仅使用系统时区

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

org.apache.maven.plugins
maven surefire插件
UTC
为什么会这样?我该如何修复它?

简短回答 Java现在先读取
user.timezone
,然后surefire在
systemPropertyVariables
中设置属性。解决方案是使用
argLine
更早地设置它:

<plugin>
  ...
  <configuration>
    <argLine>-Duser.timezone=UTC</argLine>
通常,解决方案是在命令行上指定时区,如
java-Duser.timezone=UTC TimeZoneTest
,或使用
timezone.setDefault(timezone.getTimeZone(“UTC”))编程设置它

全文:


org.apache.maven.plugins
maven surefire插件
... 如果需要,可以指定版本、其他设置。。。
-时区=UTC

@fge这是不相关的,
newdate()
只是一个可以与Java 7进行比较的简单示例。新的时区使用相同的默认时区,因此它们有完全相同的问题。奇怪的是
mvn-Duser.timezone=UTC
也不起作用。只有
有效。设置
时区.setDefault(TimeZone.getTimeZone(“UTC”)@中的code>对我有用。argLine不知道为什么。在junit4.10和mvn3add user.timezone上,在MAVEN_选项中添加例如:export MAVEN_OPTS=-Duser.timezone=GMT+8@SanghyunLee这将系统属性应用于Maven进程。默认情况下,Surefire/Failsafe将在单独的过程中运行测试。请参阅“分叉测试执行”
import java.util.*;

class TimeZoneTest {
  public static void main(String[] args) {
    System.setProperty("user.timezone", "UTC");
    System.out.println(new Date());
  }
}
  <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>