在Java8上为maven单元测试设置时区
如何在Java8上的maven surefire中设置单元测试的时区 在Java 7中,这用于处理在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
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>