2

我有一个我想测试的服务,如下所示:

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
4

1 回答 1

2

我认为您对newInstance方法的模拟不正确。应该:

mockSql.demand.newInstance { def datasource->
    return mockSql 
}

方法也一样eachRowdef在每个闭包参数之前添加。

于 2013-09-16T10:47:56.087 回答