0

我正在为调用 Date.parse() 的方法编写测试。代码看起来像这样

    public void someMethod(String s){

         Date date = new Date();

         date.setTime(Date.parse(s));

         //other methods
    }

在我的测试中,我做了这样的事情

    public void test(){
          ...

          Date date = new Date();
          someMethod(date.toString());

          //assert statements
    }

它在我的机器上运行良好,我在我的 eclipse 和 IntelliJ IDEA 中尝试过。但是,当我将它推送到我们的服务器进行构建时,测试失败了。它抛出 IllegalArgumentException。我的队友也收到了同样的例外。我们使用相同的 sdk:jdk1.7.0 和 IDE eclipse 和 IntelliJ。唯一的区别是我的机器上安装了 jre7。我还尝试了一个简单的测试:

    public void test(){
          Date d = new Date();
          String s = d.toString();

          Date e = new Date();
          e.setTime(Date.parse(s));

          sysout(e);
    } 

在我的机器上运行良好,但在他的机器上收到 IllegalArgumentException。我无法更改被测方法,因此无法执行从字符串到日期的其他转换。谢谢

编辑 - StackTrace

    java.lang.IllegalArgumentException
        at java.util.Date.parse(Date.java:598)
        at package.Class.Method(Class.java:135)
        at package.TestClass.TestMethod(TestMethod.java:192)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)
        at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
        at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:296)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284)
        at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
        at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:209)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
        at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
        at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
        at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
        at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:42)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

抱歉,我无法正确编辑它,我的机器中禁用了 javascript

4

4 回答 4

2

Date.toString()并且Date.parse()依赖于语言环境。因此,toString()' 的输出可能不会被parse(). 最好传递日期的时间戳,因为这只是一个数字,因此是唯一的。

于 2012-08-23T01:24:28.773 回答
1

Although this may not resolve your problem, i think having a look at the DateFormatter class would be very helpful for your case.

于 2012-08-23T05:05:16.997 回答
1

从JDK 1.1 开始, Javadocs已被弃用 Date.parse()

因此,您最好更改以下代码:

Date d = new Date();
String s = d.toString();

Date e = new Date();
e.setTime(Date.parse(s));

像这样:

Date d = new Date();

Date e = new Date(); 
e.setTime(d.getTime());
于 2012-08-23T02:28:31.130 回答
0

Date.parse() 已被弃用。改变方法如下,再试一次。

public void someMethod(String s){
     Date date = new Date();
     try {
      date=DateFormat.getDateInstance().parse(s);
     } catch (ParseException e) {

     e.printStackTrace();
     }


     //other methods
}
于 2012-08-23T05:57:25.640 回答