2012-08-23 74 views
0

我正在爲調用Date.parse()的方法編寫一個測試。該代碼看起來像這樣Java Date.parse IllegalArgument

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異常。我無法更改測試中的方法,因此我無法執行字符串之間的其他轉換。由於

編輯 - 堆棧跟蹤

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在我的機器上禁用

+0

您是否有確切的StackTrace錯誤?你機器上的'd.toString()'與另一臺機器上的輸出是什麼?你有不同的語言環境安裝嗎? – mercutio

+0

發佈堆棧跟蹤 – Bohemian

+0

我忘記告訴我不允許修改代碼,所以我打算將日期的時間戳作爲測試參數。如果可能的話,我還會建議我們的開發人員更新他們的代碼,因爲一些代碼包含了很久以前編寫的depricated方法。謝謝。 –

回答

2

Date.toString()Date.parse()是依賴語言環境。因此,的輸出有可能不被parse()明確可讀。最好傳遞日期的時間戳,因爲這只是一個數字,因此是唯一的。

+1

最重要的是,'Date.parse()'是[棄用](http://docs.oracle.com/javase/7/docs/api/java/util/Date.html#parse(java.lang。字符串))很久以前。 – ryanprayogo

1

由於每JavadocsDate.parse()被棄用JDK 1.1

,所以你會改變如下代碼的更好:

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

雖然這可能無法解決您的問題,我認爲看看DateFormatter課程對您的情況非常有幫助。

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 
}