0

此测试失败。我传入当前日期并使用我编写的方法将日期转换为对象。当我返回它时,因为它是一个对象。当我写我的断言时,它失败了。我在 date 上调用 tostring 方法,因为当返回对象时它就像一个字符串,因为字符串是一个对象。

@Test
   public void testOrder5() throws Exception {
      Phone input = new DefaultPhone();
      Date date = new Date();
      input.setCreatedDate(date); // passing in date object
      PhoneObj output = Book.change(input); //pases input to change method to convert date to phone object
      assertThat(output.getCreatedDate(), is(date.toString()));
   }

这是我的失败错误

java.lang.AssertionError: 
Expected: is "Wed Oct 09 12:03:17 EDT 2013"
     got: "2013-10-09T12:03:17-04:00"

at org.junit.Assert.assertThat(Assert.java:780)
at org.junit.Assert.assertThat(Assert.java:738)
at ca.on.oicr.DtosTest.testOrder5(DtosTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

在我的转换更改方法中,我有

public static PhoneObj change(Order from) {
     PhoneObj phoneobj = new OrderDto();
    if (from.getCreatedDate() != null) {
             phoneobj.setCreatedDate(dateTimeFormatter.print(from.getCreatedDate().getTime()));
          }
return phoneobj;
}

我尝试了以下建议,但没有奏效

   @Test
   public void testOrder5() throws Exception {
      Phone input = new Defaultphone();
      SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
      Date date = new Date();
      String str = date.toString();
      input.setCreatedDate(date);
      Date date1 = sf.parse(str);
      PhoneObj output = Book.change(input);
      assertThat(output.getCreatedDate(), is((date1.toString())));
   }

说日期不可解析

4

2 回答 2

1

用于SimpleDateFormat以相同的格式比较两者Date。在下面的测试中,我比较了格式化程序生成的字符串。

@Test
   public void testOrder5() throws Exception {
      SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
      Phone input = new DefaultPhone();
      Date date = new Date();
      input.setCreatedDate(date); // passing in date object
      PhoneObj output = Book.change(input); //pases input to change method to convert date to phone object
      assertThat(sf.format((Date)(output.getCreatedDate())), sf.format(date));
   }
于 2013-10-09T17:45:27.830 回答
1

您没有显示代码,但我建议您比较Date对象或比较使用相同格式String生成的对象。SimpleDateFormat

您的问题是您比较String从相同Date但格式不同的两个 s 。

于 2013-10-09T16:14:36.000 回答