Unit testing Can';t荷载“;data.yml";在测试中,java.lang.RuntimeException
我一直在玩java play框架和下面的教程。在尝试实现“使用夹具编写更复杂的测试”这一部分时,我在测试中遇到了这样一个错误:Unit testing Can';t荷载“;data.yml";在测试中,java.lang.RuntimeException,unit-testing,playframework,Unit Testing,Playframework,我一直在玩java play框架和下面的教程。在尝试实现“使用夹具编写更复杂的测试”这一部分时,我在测试中遇到了这样一个错误: A java.lang.RuntimeException has been caught, Cannot load fixture data.yml: play.exceptions.UnexpectedException: Unexpected Error In /test/BasicTest.java, line 137 : Fixtures.loadModels
A java.lang.RuntimeException has been caught, Cannot load fixture data.yml: play.exceptions.UnexpectedException: Unexpected Error
In /test/BasicTest.java, line 137 :
Fixtures.loadModels("data.yml");
堆栈跟踪:
java.lang.RuntimeException: Cannot load fixture data.yml: play.exceptions.UnexpectedException: Unexpected Error
at play.test.Fixtures.loadModels(Fixtures.java:221)
at BasicTest.fullTest(BasicTest.java:137)
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.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at play.test.PlayJUnitRunner$StartPlay$2$1.evaluate(PlayJUnitRunner.java:98)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:48)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at play.test.TestEngine.run(TestEngine.java:101)
at controllers.TestRunner.run(TestRunner.java:67)
at sun.reflect.GeneratedMethodAccessor145.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:540)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:498)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:474)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:469)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:157)
at play.server.PlayHandler$NettyInvocation.execute(PlayHandler.java:237)
at play.Invoker$Invocation.run(Invoker.java:265)
at play.server.PlayHandler$NettyInvocation.run(PlayHandler.java:217)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.RuntimeException: play.exceptions.UnexpectedException: Unexpected Error
at play.db.jpa.GenericModel.create(GenericModel.java:50)
at play.db.jpa.JPAPlugin.bind(JPAPlugin.java:81)
at play.plugins.PluginCollection.bind(PluginCollection.java:469)
at play.data.binding.Binder.bind(Binder.java:309)
at play.data.binding.Binder.bind(Binder.java:302)
at play.test.Fixtures.loadModels(Fixtures.java:196)
... 52 more
Caused by: play.exceptions.UnexpectedException: Unexpected Error
at play.db.jpa.GenericModel.edit(GenericModel.java:156)
at play.db.jpa.GenericModel.create(GenericModel.java:48)
... 57 more
Caused by: play.exceptions.UnexpectedException: Unexpected Error
at play.data.binding.BeanWrapper$Property.setValue(BeanWrapper.java:230)
at play.data.binding.BeanWrapper.bind(BeanWrapper.java:70)
at play.db.jpa.GenericModel.edit(GenericModel.java:153)
... 58 more
Caused by: java.lang.IllegalArgumentException: Can not set java.sql.Date field models.Post.postedAt to java.util.Date
at play.data.binding.BeanWrapper$Property.setValue(BeanWrapper.java:225)
at play.data.binding.BeanWrapper.bind(BeanWrapper.java:70)
at play.db.jpa.GenericModel.edit(GenericModel.java:153)
at play.db.jpa.GenericModel.create(GenericModel.java:48)
at play.db.jpa.JPAPlugin.bind(JPAPlugin.java:81)
at play.plugins.PluginCollection.bind(PluginCollection.java:469)
at play.data.binding.Binder.bind(Binder.java:309)
at play.data.binding.Binder.bind(Binder.java:302)
at play.test.Fixtures.loadModels(Fixtures.java:196)
at BasicTest.fullTest(BasicTest.java:137)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at play.test.PlayJUnitRunner$StartPlay$2$1.evaluate(PlayJUnitRunner.java:98)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:48)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at play.test.TestEngine.run(TestEngine.java:101)
at controllers.TestRunner.run(TestRunner.java:67)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:540)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:498)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:474)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:469)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:157)
at Invocation.HTTP Request(Play!)
发生错误的行:
@Test
public void fullTest() {
Fixtures.loadModels("data.yml");
// count everyrhing
assertEquals(2, User.count());
我也有
@Before
public void setup() {
Fixtures.deleteDatabase();
}
但这没用
data.yml文件包含此信息,它应该是有效的,因为它是从官方网站下载的
# Test data
User(bob):
email: bob@gmail.com
password: secret
fullname: Bob
isAdmin: true
User(jeff):
email: jeff@gmail.com
password: secret
fullname: Jeff
Post(firstBobPost):
title: About the model layer
postedAt: 2009-06-14
author: bob
content: >
The model has a central position in a Play! application. It is the domain-specific
representation of the information on which the application operates.
Martin fowler defines it as:
Responsible for representing concepts of the business, information about the
business situation, and business rules. State that reflects the business situation
is controlled and used here, even though the technical details of storing it are
delegated to the infrastructure. This layer is the heart of business software.
Post(secondBobPost):
title: Just a test of YABE
postedAt: 2009-03-25
author: bob
content: >
Well, it's just a test.
Post(jeffPost):
title: The MVC application
postedAt: 2009-06-06
author: jeff
content: >
A Play! application follows the MVC architectural pattern as applied to the
architecture of the Web.
This pattern splits the application into separate layers: the Presentation
layer and the Model layer. The Presentation layer is further split into a
View and a Controller layer.
Comment(c1):
author: Guest
content: >
You are right !
postedAt: 2009-06-14
post: firstBobPost
Comment(c2):
author: Mike
content: >
I knew that ...
postedAt: 2009-06-15
post: firstBobPost
Comment(c3):
author: Tom
content: >
This post is useless ?
postedAt: 2009-04-05
post: secondBobPost
我犯了什么错误?解决了。
问题是yaml文件中的日期格式。
那是2009年6月6日,我刚刚进入2009年6月6日,一切都开始运转。我知道你说你通过改变格式解决了这个问题,但我相信这里还有另一个问题。在您的日志中有以下消息:
Can not set java.sql.Date field models.Post.postedAt to java.util.Date
在我看来,您的Post类包含了java.sql.Date而不是java.util.Date,这可能是问题的根源(格式化是真正冲突的一个障碍)您确定它正在查找data.yml吗?请接受这个答案(即使这是您自己的答案)向社区表明这不再是一个问题。谢谢,这才是这个问题的真正解决方案。