简体   繁体   中英

Spock's groovy test fails with ArrayIndexOutOfBoundsException at Stub()

I have a java class AgentConverter as which implements org.springframework.core.convert.converter.Converter -

public class AgentConverter implements Converter<RequestWrapper, TicketingAgent> {

    public TicketingAgent convert(RequestWrapper wrapper) {

        TicketingAgent agent = new TicketingAgent();
        ....
        ....

Then I have java class which is using above AgentConverter as -

public class BuildTicketingDocumentRequest implements BuildRequest<TicketingDocumentRequest> {

    private final PosConverter posConverter;
    private final AgentConverter agentConverter;    //here it is ..
    private final TransactionInfoConverter transactionInfoConverter;
    private final BuildRequestComponent<TicketingDocument> buildRequestComponent;

I have written Groovy test case where I am stubbing the AgentConverter class -

class ConversionStrategyForDocCreateTest extends EdiSimulator  {

    def "Master Test for Build Conversion Strategy to create TicketingDocumentRequest"() {

        given:"Mocked Classes"
        AgentConverter agentConverter = Stub(AgentConverter.class)     // this line exception comes 
        agentConverter.convert(wrapper) >> new TicketingAgent()        
        .....
        .....

Now when above test case - I am getting:

java.lang.ArrayIndexOutOfBoundsException: 45569

    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:91)
    at org.spockframework.mock.runtime.ProxyBasedMockFactory.create(ProxyBasedMockFactory.java:49)
    at org.spockframework.mock.runtime.JavaMockFactory.create(JavaMockFactory.java:51)
    at org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java:44)
    at org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:45)
    at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:281)
    at org.spockframework.lang.SpecInternals.StubImpl(SpecInternals.java:131)
    at com.sabre.ticketing.hub.converter.startegy.ConversionStrategyForDocCreateMasterTest.Master Test for Build Conversion Strategy to create TicketingDocumentRequest(ConversionStrategyForDocCreateMasterTest.groovy:35)

While hit & trial I found if I remove AgentConverter's implements Converter like below -

public class AgentConverter {             // Here i have removed "implements Converter"

    public TicketingAgent convert(RequestWrapper wrapper) {

        TicketingAgent agent = new TicketingAgent();

then things start working and there is no java.lang.ArrayIndexOutOfBoundsException . I am trying to understand what is the relation between Stub() in Spock framework AND stubbed class's interface implementation. Couldn't find in google so posted here... Any help is appreciated.

Here is my POM snippet for spock & groovy versions:

  <dependency>
       <groupId>org.spockframework</groupId>
       <artifactId>spock-core</artifactId>
       <version>1.0-groovy-2.4</version>
       <scope>test</scope>
   </dependency>
   <dependency>
       <groupId>org.codehaus.groovy</groupId>
       <artifactId>groovy-all</artifactId>
       <version>2.4.4</version>
       <scope>test</scope>
   </dependency>

Disclaimer: I used spock ~ 1.5 years ago, maybe things have changed since then...

Anyway:

Are you running with Java 9?

If so, check this cglib issue :

All-in-all cglib seems to have an issue, you can try to use bytebuddy instead as suggested in this thread.

Another possible solution is refactoring:

Maybe BuildTicketingDocumentRequest can depend on interface rather than concrete implementation like AgentConverter . When the mock is created out of the interface, it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM