Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Always getting exception "Wrong type at constant pool index" with Cucumber-Java8

I am trying to set-up an example project for the Java8 dialect of Cucumber. My problem is, that I don't get it running. I always get the following hierarchy of exceptions:

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

I have no clue why I get this error nor how to fix it.

I have packaged everything in a Maven project. The layout is like that:

./src/test/java/soy/wimmer/CucumberIT.java
./src/test/java/soy/wimmer/CucumberStepdefs.java
./src/test/resources/cucumber/cucumber-java8.feature
./pom.xml

The dependencies I include in the pom.xml are:

<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>

Additionally the pom.xml only loads the compiler and the failsafe plugin.

My definition of 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 {                                                        
}        

My feature definition:

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  

And this are my step definitions:

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();                                        
        });                                                                      
    }                                                                            
}

Any idea what I'm doing wrong here?

like image 653
Matthias Wimmer Avatar asked Sep 22 '15 22:09

Matthias Wimmer


2 Answers

The problem is caused because the Java8 dialect of Cucumber uses implementation details of Oracle's JDK8.

I was using OpenJDK8 as packaged by Debian which causes a different organisation of the constant pool. When I try the same with Oracle's JDK8 everything works as expected.

If you want to try it yourself, I published the complete example project on github: https://github.com/mawis/cucumber-java8-test

I also reported a bug at the issue tracker of cucumber-jvm here: https://github.com/cucumber/cucumber-jvm/issues/912

You might check the issue tracker to see if the problem will have been fixed in the future.

For now if you want to use cucumber-java8 it seems you have to use Oracle's implementation of the JDK.

(The fame for solving this problem belongs to Holger with his comments to the question. I just wanted to write this answer as a summary.)

like image 72
Matthias Wimmer Avatar answered Oct 31 '22 21:10

Matthias Wimmer


Just use 1.2.5 version which has been recently released. It solved the bug referenced by accepted answer.

like image 38
M4ks Avatar answered Oct 31 '22 21:10

M4ks