Mocking of concrete class failing with Spock 2.0

陌路散爱 提交于 2020-06-01 05:05:46

问题


Mocking of external concrete class fails with the below mentioned error.

java.lang.AbstractMethodError: Receiver class me.spike.LibraryTest does not define or inherit an implementation of the resolved method 'abstract java.lang.Object getProperty(java.lang.String)' of interface groovy.lang.GroovyObject.

I've tried adding cglibs-nodep and objenesis but was not successful in getting to mock the concrete class.

The repository mentioned here has a failing test. The test itself nonsensical. The intent is to get the mock working.

MCVE - https://github.com/ajaydivakaran/spock_spike


回答1:


Like I said in the other question, you should get rid of the Build Helper plugin because Maven will recognise your src/test/groovy automatically (well, at least as long as you src/test/java is not completely empty). Also the Surefire plugin is over-specified as I also told you before. You should keep your build files small and only include what is needed.

But the real problem is that your Groovy Eclipse batch compiler is version 3.0.3 while you use Groovy version 2.5.11. Just downgrade that one dependency to fit your Groovy version and your test runs normally. Or go the other way, upgrade Groovy and Spock to 3.0. Anyway, changing this line fixes your build:

<groovy-eclipse-batch.version>2.5.11-01</groovy-eclipse-batch.version>



回答2:


While this is just a workaround, you may switch to gmavenplus to handle Groovy compilation instead of groovy-eclipse-compiler and then that test passes:

            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.8.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compileTests</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <version>2.5.11</version>
                        <scope>runtime</scope>
                        <type>pom</type>
                    </dependency>
                </dependencies>
            </plugin>

It definitely seems to be Groovy Eclipse Compiler related. We don't test Spock with GEC, nor it is officially supported. That test fails also with Spock 1.3-groovy-2.5 (and GEC), so it is not a regression in 2.0. However, may be related to the "hacks" in JavaMockInterceptor and some issues with Groovy 3 - #1076.

You may report it in the Spock issue tracker, however, I don't know if it will be properly handled.



来源:https://stackoverflow.com/questions/61902037/mocking-of-concrete-class-failing-with-spock-2-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!