简体   繁体   English

Spock 的 groovy 测试在 Stub() 出现 ArrayIndexOutOfBoundsException 失败

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

I have a java class AgentConverter as which implements org.springframework.core.convert.converter.Converter -我有一个 java class AgentConverter实现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 -然后我有 java class 使用上面的AgentConverter作为 -

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 -我已经编写了 Groovy 测试用例,我在其中存根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 -在命中和试用时,我发现如果我删除 AgentConverter 的实现 Converter ,如下所示 -

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 .然后事情开始工作并且没有java.lang.ArrayIndexOutOfBoundsException I am trying to understand what is the relation between Stub() in Spock framework AND stubbed class's interface implementation.我试图了解 Spock 框架中的Stub()和存根类的接口实现之间的关系。 Couldn't find in google so posted here... Any help is appreciated.无法在谷歌找到所以张贴在这里...任何帮助表示赞赏。

Here is my POM snippet for spock & groovy versions:这是我的 spock 和 groovy 版本的 POM 片段:

  <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...免责声明:我使用 spock ~ 1.5 年前,也许从那以后事情发生了变化......

Anyway:反正:

Are you running with Java 9?您是否正在使用 Java 9 运行?

If so, check this cglib issue :如果是这样,请检查此cglib 问题

All-in-all cglib seems to have an issue, you can try to use bytebuddy instead as suggested in this thread.总而言之 cglib 似乎有一个问题,您可以尝试使用 bytebuddy 来代替此线程中的建议。

Another possible solution is refactoring:另一种可能的解决方案是重构:

Maybe BuildTicketingDocumentRequest can depend on interface rather than concrete implementation like AgentConverter .也许BuildTicketingDocumentRequest可以依赖于接口而不是像AgentConverter这样的具体实现。 When the mock is created out of the interface, it should work.当从界面创建模拟时,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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