编写一个简单的junit / spring-test dbunit测试来测试我的jpa实体持久性。奇怪的事情发生了。当dbunit将我的数据集与@ExpectedDatabase文件进行比较时,它会抛出比较joda datetime字段的异常:
junit.framework.ComparisonFailure: value (table=person, row=2, col=birth_date) expected:<1973-11-2[7]> but was:<1973-11-2[6 23:00:00.0]>
at org.dbunit.assertion.JUnitFailureFactory.createFailure(JUnitFailureFactory.java:39)
at org.dbunit.assertion.DefaultFailureHandler.createFailure(DefaultFailureHandler.java:105)
at org.dbunit.assertion.DefaultFailureHandler.handle(DefaultFailureHandler.java:208)
at org.dbunit.assertion.DbUnitAssert.compareData(DbUnitAssert.java:524)
at org.dbunit.assertion.DbUnitAssert.assertEquals(DbUnitAssert.java:409)
at org.dbunit.assertion.DbUnitAssert.assertEquals(DbUnitAssert.java:253)
at org.dbunit.assertion.DbUnitAssert.assertEquals(DbUnitAssert.java:205)
at org.dbunit.Assertion.assertEquals(Assertion.java:104)
at com.github.springtestdbunit.assertion.DefaultDatabaseAssertion.assertEquals(DefaultDatabaseAssertion.java:33)
at com.github.springtestdbunit.DbUnitRunner.verifyExpected(DbUnitRunner.java:115)
at com.github.springtestdbunit.DbUnitRunner.afterTestMethod(DbUnitRunner.java:67)
at com.github.springtestdbunit.DbUnitTestExecutionListener.afterTestMethod(DbUnitTestExecutionListener.java:158)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:395)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:91)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
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)
我坚持“1973-11-27”,但dbunit将其视为“1973-11-26 23:00:00.0”。 (一小时前)。为什么?看起来像带有时区的东西。
这是我的测试类:
@Test
@ExpectedDatabase (value = "classpath:person-expected-data.xml")
public void testSavePersonEM() {
logger.info("--- testSavePersonEM ---");
Person person = new Person("Joe", "King");
person.setAddress("XXXX XXXXX 27");
person.setEmail("joe@hotmail.com");
person.setTelephone("123456789");
person.setBirthDate(new DateTime("1973-11-27"));
em.persist(person);
assertEquals(new Long(2), person.getId());
assertEquals("1973-11-27", person.getBirthDate().toString("yyyy-MM-dd"));
logger.info("new id:" + person.getId());
logger.info("birthdate: " + person.getBirthDate().toString());
}
logger.info显示“1973-11-27”。
这是person-expected-data.xml:
<dataset>
<Person id="2" first_name="joe" last_name="king"
birth_date="1973-11-27" address="bla bla 27" city="bla"
country="Serbia" email="joe@hotmail.com" telephone="123456798" />
</dataset>
答案 0 :(得分:0)
好的,我通过将实体的字段数据类型从org.joda.time.DateTime更改为org.joda.time.LocalDate来找到解决方案。此刻我不关心时间或时区。我只需要约会:
...
@Column(name = "birth_date")
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private **LocalDate** birthDate;
...
现在测试运行没有问题。