java-8 - 始终使用Cucumber-Java8获取异常 "Wrong type at constant pool index"

标签 java-8 cucumber bdd cucumber-jvm cucumber-java

我正在尝试为Cucumber的Java8方言建立示例项目。我的问题是,我没有让它运行。我总是得到以下异常层次结构:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.068 sec <<< FAILURE! - in soy.wimmer.CucumberIT
Feature: Cucumber with Java8  Time elapsed: 0.051 sec  <<< ERROR!
cucumber.runtime.CucumberException: Failed to instantiate class soy.wimmer.CucumberStepdefs
[…]
Caused by: java.lang.reflect.InvocationTargetException: null
[…]
Caused by: cucumber.runtime.CucumberException: java.lang.IllegalArgumentException: Wrong type at constant pool index
[…]
Caused by: java.lang.IllegalArgumentException: Wrong type at constant pool index
    at sun.reflect.ConstantPool.getMemberRefInfoAt0(Native Method)
    at sun.reflect.ConstantPool.getMemberRefInfoAt(ConstantPool.java:47)
    at cucumber.runtime.java8.ConstantPoolTypeIntrospector.getTypeString(ConstantPoolTypeIntrospector.java:37)
    at cucumber.runtime.java8.ConstantPoolTypeIntrospector.getGenericTypes(ConstantPoolTypeIntrospector.java:27)
    at cucumber.runtime.java.Java8StepDefinition.<init>(Java8StepDefinition.java:45)
    at cucumber.runtime.java.JavaBackend.addStepDefinition(JavaBackend.java:162)
    at cucumber.api.java8.En.Given(En.java:190)
    at soy.wimmer.CucumberStepdefs.<init>(CucumberStepdefs.java:8)
[…]

Results :

Tests in error: 
  Failed to instantiate class soy.wimmer.CucumberStepdefs

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

我不知道为什么会收到此错误,也不知道如何解决它。

我已经将所有内容打包在一个Maven项目中。布局是这样的:
./src/test/java/soy/wimmer/CucumberIT.java
./src/test/java/soy/wimmer/CucumberStepdefs.java
./src/test/resources/cucumber/cucumber-java8.feature
./pom.xml

我包含在pom.xml中的依赖项是:
<dependencies>                                                               
    <dependency>                                                             
        <groupId>info.cukes</groupId>                                        
        <artifactId>cucumber-java8</artifactId>                              
        <version>1.2.3</version>                                             
        <scope>test</scope>                                                  
    </dependency>                                                            

    <dependency>                                                             
        <groupId>info.cukes</groupId>                                        
        <artifactId>cucumber-junit</artifactId>                              
        <version>1.2.3</version>                                             
        <scope>test</scope>                                                  
    </dependency>                                                            

    <dependency>                                                             
        <groupId>junit</groupId>                                             
        <artifactId>junit</artifactId>                                       
        <version>4.12</version>                                              
        <scope>test</scope>                                                  
    </dependency>                                                            
</dependencies>

另外,pom.xml仅加载编译器和故障安全插件。

我对CucumberIT.java的定义:
package soy.wimmer;                                                              

import cucumber.api.CucumberOptions;                                             
import cucumber.api.junit.Cucumber;                                              
import org.junit.runner.RunWith;                                                 

@RunWith(Cucumber.class)                                                         
@CucumberOptions(features = "classpath:cucumber")                                
public class CucumberIT {                                                        
}        

我的功能定义:
Feature: Cucumber with Java8                                                     
        As a developer                                                           
        I want to use Cucumber-java8                                             
        So that I have nicer step definitions                                    

        Scenario: Let's try it                                                   
                Given I have some dummy code                                     
                When I try to test it                                            
                Then it should work with cucumber-java8  

这是我的步骤定义:
package soy.wimmer;                                                              

import cucumber.api.PendingException;                                            
import cucumber.api.java8.En;                                                    

public class CucumberStepdefs implements En {                                    
    public CucumberStepdefs() {                                                  
        Given("^I have some dummy code$", () -> {                                
            // Write code here that turns the phrase above into concrete actions 
            throw new PendingException();                                        
        });                                                                      

        When("^I try to test it$", () -> {                                       
            // Write code here that turns the phrase above into concrete actions 
            throw new PendingException();                                        
        });                                                                      

        Then("^it should work with cucumber-java(\\d+)$", (Integer arg1) -> {    
            // Write code here that turns the phrase above into concrete actions 
            throw new PendingException();                                        
        });                                                                      
    }                                                                            
}

知道我在这里做错了什么吗?

最佳答案

引起该问题的原因是,Cucumber的Java8方言使用了Oracle JDK8的实现细节。

我使用的是Debian打包的OpenJDK8,它导致了常量池的不同组织。当我尝试使用Oracle JDK8时,一切都会按预期进行。

如果您想自己尝试,我将完整的示例项目发布在github上:https://github.com/mawis/cucumber-java8-test

我还在这里的cumulage-jvm的问题跟踪器中报告了一个错误:https://github.com/cucumber/cucumber-jvm/issues/912

您可以检查问题跟踪器,以查看将来是否已解决问题。

现在,如果您想使用Cucumber-java8,似乎您必须使用Oracle的JDK实现。

(解决此问题的名声属于霍尔格,他对问题有评论。我只是想将此答案写为总结。)

关于java-8 - 始终使用Cucumber-Java8获取异常 "Wrong type at constant pool index",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32728342/

相关文章:

jquery - Jasmine jQuery : Using toBeHidden() and toBeVisible()

java - 如何修复 "Unable to read repository at https://qmetry.github.io/qaf/editor/bdd/eclipse/content.xml.java.util.NoSuchElementException"。异常(exception)

scala - JDK 1.8 - 不能导入 sbt 项目

java - 反转 Collectors.toMap 以添加到 ArrayList

cucumber - 在场景大纲下具有不同数量项目的数据表,并在 Cucumber 中提供多个示例。是否可以?

selenium - 如何从 Java 代码而不是 JUnit Runner 运行 cucumber 功能文件

Java8 流线并通过终端线上的操作进行聚合

java-8 - 使用身份组合器并行流减少

java - junit cucumber 缺少步骤错误

javascript - 使用 Typescript 检索 Mocha 测试中当前测试的名称