代码之家  ›  专栏  ›  技术社区  ›  Manu Artero Rajeev Dutta

模拟spock中扩展抽象类的类

  •  0
  • Manu Artero Rajeev Dutta  · 技术社区  · 6 年前

    我有以下扩展抽象类的类

    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 我想嘲笑我的班级:

    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 回复  |  直到 6 年前
        1
  •  1
  •   kriegaex    6 年前

    在类路径上除了cglib外还有objensis,as 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"
      }
    }