gpt4 book ai didi

java - 在 Spock 中模拟扩展抽象类的类

转载 作者:行者123 更新时间:2023-12-02 11:13:27 25 4
gpt4 key购买 nike

我有以下扩展抽象类的类

public class MyConverter extends AbstractConverter<A, B> {
@Override
public B convert(A source) {
// implementation here
}
}

AbstractConverter 来自org.modelmapper,它的声明如下:

public abstract class AbstractConverter<S, D> implements Converter<S, D> { 
...
}

现在,从使用 Spock 的 Groovy 文件我想模拟我的类(class):

class ATestOverThere extends Specification {
def myConverter = Mock(MyConverter) // THIS THROWS THE EXCEPTION

...
}

但是在初始化模拟时我遇到了以下异常。

java.lang.IllegalArgumentException
at net.sf.cglib.proxy.BridgeMethodResolver.resolveAll(BridgeMethodResolver.java:61)
at net.sf.cglib.proxy.Enhancer.emitMethods(Enhancer.java:911)
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:498)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
at org.spockframework.mock.runtime.ProxyBasedMockFactory$CglibMockFactory.createMock(ProxyBasedMockFactory.java:154)
at org.spockframework.mock.runtime.ProxyBasedMockFactory.create(ProxyBasedMockFactory.java:68)
at org.spockframework.mock.runtime.JavaMockFactory.createInternal(JavaMockFactory.java:59)
at org.spockframework.mock.runtime.JavaMockFactory.create(JavaMockFactory.java:40)
at org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java:44)
at org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:51)
at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:296)
at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:286)
at org.spockframework.lang.SpecInternals.MockImpl(SpecInternals.java:105)
at com.tuenti.services.argentina.business.products.sva.SvaManagerSpec.$spock_initializeFields(SvaManagerSpec.groovy:14)
<小时/>

似乎我无法模拟一个使用 Spock 扩展抽象类的类,可以吗?

最佳答案

在类路径上除了 CGLIB 之外还使用 Objenesis,如 Michael Easter正确地说,你的例子有效。不过,无需使用 ByteBuddy。

另请参阅Spock manual .

<!-- (...) -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>2.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.0.0</version>
<scope>compile</scope>
</dependency>
<!-- (...) -->
package de.scrum_master.stackoverflow;

import org.modelmapper.AbstractConverter;

public class MyConverter extends AbstractConverter<Integer, String> {
@Override
protected String convert(Integer source) {
return source.toString();
}
}
package de.scrum_master.stackoverflow

import spock.lang.Specification

class MyConverterTest extends Specification {
def "test"() {
given:
def myConverter = Mock(MyConverter) {
convert(_) >>> ["one", "two"] >> { callRealMethod() }
}

expect:
myConverter.convert(11) == "one"
myConverter.convert(22) == "two"
myConverter.convert(11) == "11"
myConverter.convert(22) == "22"
}
}

关于java - 在 Spock 中模拟扩展抽象类的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50445780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com