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

时间:2018-05-21 09:31:15

标签: java groovy mocking abstract-class spock

我有以下扩展Abstract类的类

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 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扩展抽象类的类,是吗?

1 个答案:

答案 0 :(得分:1)

在类路径上除了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"
  }
}