我有一项服务,我想测试如下:
class MyService {
def dataSource
def method1(id) {
Sql sql = Sql.newInstance(dataSource)
def query = "select * from some_table where id = ?"
sql.eachRow(query, [id]) { row ->
// do stuff with row
}
}
}
我试图用MockFor
来测试这个class MyServiceTest extends GroovyTestCase {
@Test
void testMethod1() {
def mockResults = [
[ id:1, name:"foo", ...]
]
def mockSql = new MockFor(Sql.class)
mockSql.demand.newInstance { mockSql }
mockSql.demand.eachRow { query, params, closure ->
// run the closure over the mock array
mockResults.each(closure)
}
mockSql.use {
MyService myService = new MyService()
myService.method1(1)
}
}
}
但我得到以下例外:
junit.framework.AssertionFailedError: No call to 'newInstance' expected at this point. Still 1 call(s) to 'eachRow' expected.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:71)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:81)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
at groovy.mock.interceptor.StrictExpectation.match(StrictExpectation.groovy:56)
at groovy.mock.interceptor.StrictExpectation$match.call(Unknown Source)
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 groovy.mock.interceptor.MockInterceptor.beforeInvoke(MockInterceptor.groovy:31)
at groovy.mock.interceptor.MockProxyMetaClass.invokeStaticMethod(MockProxyMetaClass.java:98)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:50)
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.bnpparibas.client1st.service.MetricsService.getClientDocumentsStatistic(MetricsService.groovy:483)
at com.bnpparibas.client1st.service.MetricsService$getClientDocumentsStatistic.call(Unknown Source)
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:128)
at MyServiceTest$_testMethod1_closure
答案 0 :(得分:2)
我认为您对newInstance
方法的模拟不正确。应该是:
mockSql.demand.newInstance { def datasource->
return mockSql
}
eachRow
方法相同。在每个闭包参数之前添加def
。