grails中的单元测试:“前面方法调用getMetaClass()的缺少行为定义”

时间:2014-04-07 09:24:14

标签: unit-testing grails easymock

我使用grails 2.2,我尝试用easymock测试一个类。 我的类名为Task,包含一个名为Service的服务,我想在我的测试中进行模拟。

void test(){
    Task task = new Task()
    Service service = createMock(Service.class)
    task.service = service

    expect(service.call(10)).andReturn(true)
    replay(service)

    task.execute()
}

以下是方法调用的代码:

boolean call(Long id){}

但是,系统地说,当我在我的IDE中运行测试或使用grails提供的test-app命令时,我有以下错误:

java.lang.IllegalStateException: missing behavior definition for the preceding method call getMetaClass()
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:43)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:73)
at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:92)
at com.Service$$EnhancerByCGLIB$$fa4a7672.getMetaClass(<generated>)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.checkCall(PogoMetaClassSite.java:59)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:36)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at com.myApp.Test.test(MyTest.groovy:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
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.internal.runners.statements.ExpectException.evaluate(ExpectException.java:22)
... 18 more

我的测试似乎有误,但我看不出我做错了什么。 你能救我吗?

0 个答案:

没有答案