Spock中的运行时异常模拟groovy.sql [英] Runtime Exception Mocking groovy.sql in Spock

查看:120
本文介绍了Spock中的运行时异常模拟groovy.sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了寻找能够在Spock规范中运行此代码的正确配置/语法,我一直在StackOverflow和Google周围搜寻:

I've scoured around StackOverflow and Google for a while trying to find the right configuration/syntax for being able to run this code in a Spock Specification:

Sql mockSql = Mock()

但是,当我运行单元测试时,CGLIB抛出了一个令人讨厌的异常:

However when I run the unit test, I get a nasty exception thrown in my face by CGLIB:

java.lang.IllegalAccessError: tried to access method groovy.sql.Sql.<init>()V from class groovy.sql.Sql$$EnhancerByCGLIB$$d0b7cd7f
at org.spockframework.mock.runtime.MockInstantiator.instantiate(MockInstantiator.java:33)
at org.spockframework.mock.runtime.ProxyBasedMockFactory$CglibMockFactory.createMock(ProxyBasedMockFactory.java:92)
at org.spockframework.mock.runtime.ProxyBasedMockFactory.create(ProxyBasedMockFactory.java:49)
at org.spockframework.mock.runtime.JavaMockFactory.create(JavaMockFactory.java:51)
at org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java:44)
at org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:47)
at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:282)
at org.spockframework.lang.SpecInternals.MockImpl(SpecInternals.java:83)

我查看了这个问题/答案-使用GroovyMock或类似方法模拟静态方法在Spock中-希望它可以为我提供一个很好的起点,但是对于我而言,该类是在groovy.Sql中被嘲笑的,所以它是一个groovy类,因此我不确定它是否是正确的起点.

I looked at this question/answer - Mock static method with GroovyMock or similar in Spock - hoping it might give me a good starting point, but the class being mocked in my case, groovy.Sql IS a groovy class, so I'm not sure it's the right place to start.

我已经使用GroovyMock在其他单元测试中成功测试了Groovy.Sql,但是我希望也许我只是错过了可以做到的东西,这样我才能让Spock magic和Groovy SQL一起开心.如果我只是在某个地方错过了答案,我深表歉意-希望我不会重复别人的询问.

I've successfully tested Groovy.Sql in other unit tests using GroovyMock, but I was hoping maybe I just missed something that can make it so that I can have Spock magic and Groovy SQL all happy together. My apologies if I just missed the answer somewhere - hopefully I'm not repeating someone else's inquiry.

推荐答案

使用Spock模拟类(而不是接口)时,建议将objenesis-1.2或更高版本放在测试运行时类路径上(除CGLIB之外).否则,Spock将无法避免执行要模拟的类的构造函数,这可能会带来不良后果.

When mocking classes (rather than interfaces) with Spock, putting objenesis-1.2 or higher on the test runtime class path is recommended (in addition to CGLIB) . Otherwise, Spock won't be able to avoid executing the constructor of the class to be mocked, which may have undesired side effects.

这篇关于Spock中的运行时异常模拟groovy.sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆