简体   繁体   English

Spock中的运行时异常模拟groovy.sql

[英]Runtime Exception Mocking groovy.sql in Spock

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: 我在StackOverflow和Google周围搜寻了一段时间,试图找到能够在Spock规范中运行此代码的正确配置/语法:

Sql mockSql = Mock()

However when I run the unit test, I get a nasty exception thrown in my face by CGLIB: 但是,当我运行单元测试时,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)

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或Spock中的类似方法模拟静态方法 -希望它可以为我提供一个很好的起点,但是在我的情况下,该类被嘲笑为groovy.Sql是一个groovy类,所以我不是确保这是正确的起点。

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. 我已经使用GroovyMock在其他单元测试中成功测试了Groovy.Sql,但是我希望也许我只是错过了可以做到的东西,这样我才能让Spock magic和Groovy SQL一起开心。 My apologies if I just missed the answer somewhere - hopefully I'm not repeating someone else's inquiry. 如果我只是在某个地方错过了答案,我深表歉意-希望我不会重复别人的询问。

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) . 当使用Spock模拟类(而不是接口)时,建议将objenesis-1.2或更高版本放在测试运行时类路径上(除了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将无法避免执行要模拟的类的构造函数,这可能会产生不良后果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM