最新的Groovy稳定版本令人困惑的问题

时间:2018-11-03 16:57:04

标签: testing gradle groovy mocking

我刚开始一个新的Gradle项目。在我以前的build.gradles中,我一直这样:

compile 'org.codehaus.groovy:groovy-all:2.4.15'
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'

...以及这些依赖项:

testCompile 'net.bytebuddy:byte-buddy:1.6.11'
testCompile 'org.objenesis:objenesis:2.6'

通过反复试验,我发现具有这些Bytebuddy和Objensis依赖项的Groovy 2.4.15使我能够模拟BufferedReader。在我想模拟用户输入到控制台的控制台应用程序中,这被证明是有用的。因此,“控制台处理程序”类具有以下字段/属性:

def br = new BufferedReader( new InputStreamReader(System.in, 'UTF-8' ))

在应用程序类中按以下方式使用以获取用户控制台输入:

String response = br.readLine().trim()

...表示Spock测试可以执行以下操作:

def 'prompt should show help on entering H'()  {
    given:
    consoleHandler.br = Mock( BufferedReader )
    consoleHandler.br.readLine() >> 'h'

即在控制台上模拟字母h的输入。

...但是它不适用于Groovy 2.5.3及其匹配的Spock依赖项:对于这个新项目,我输入了:

compile 'org.codehaus.groovy:groovy-all:2.5.3'
testCompile 'org.spockframework:spock-core:1.2-groovy-2.5'

...具有相同的ByteBuddy和Objenisis依赖项。我遇到以下测试失败:

  

java.lang.IllegalArgumentException:无法在以下位置创建类型   net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:140)位于   net.bytebuddy.TypeCache $ WithInlineExpunction.findOrInsert(TypeCache.java:346)     在net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:161)在   net.bytebuddy.TypeCache $ WithInlineExpunction.findOrInsert(TypeCache.java:355)     在   org.spockframework.mock.runtime.ByteBuddyMockFactory.createMock(ByteBuddyMockFactory.java:41)     在   org.spockframework.mock.runtime.ProxyBasedMockFactory.create(ProxyBasedMockFactory.java:42)     在   org.spockframework.mock.runtime.JavaMockFactory.createInternal(JavaMockFactory.java:58)     在   org.spockframework.mock.runtime.JavaMockFactory.create(JavaMockFactory.java:38)     在   org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java:42)     在   org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:46)     在   org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:294)     在   org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:284)     在   org.spockframework.lang.SpecInternals.MockImpl(SpecInternals.java:100)     在核心上。FirstSpec.setup(first_tests.groovy:20)

原因:   java.lang.NoSuchMethodError:   net.bytebuddy.dynamic.loading.ClassInjector $ UsingLookup.isAvailable()Z     在   org.spockframework.mock.runtime.ByteBuddyMockFactory.determineBestClassLoadingStrategy(ByteBuddyMockFactory.java:103)     在   org.spockframework.mock.runtime.ByteBuddyMockFactory.access $ 300(ByteBuddyMockFactory.java:27)     在   org.spockframework.mock.runtime.ByteBuddyMockFactory $ 1.call(ByteBuddyMockFactory.java:54)     在   org.spockframework.mock.runtime.ByteBuddyMockFactory $ 1.call(ByteBuddyMockFactory.java:43)     在net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)

那里有Groovyüber怪胎吗?

1 个答案:

答案 0 :(得分:6)

您必须升级byte-buddy

testCompile 'net.bytebuddy:byte-buddy:1.8.21'

Spock版本1.1-groovy-2.4取决于byte-buddy:1.6.5-https://mvnrepository.com/artifact/org.spockframework/spock-core/1.1-groovy-2.4

Spock版本1.2-groovy-2.5取决于byte-buddy:1.8.21-https://mvnrepository.com/artifact/org.spockframework/spock-core/1.2-groovy-2.5