Tôi đang cố gắng thiết lập dự án mẫu cho phương ngữ Java8 của Cucumber. Vấn đề của tôi là, tôi không làm cho nó chạy. Tôi luôn nhận được hệ thống phân cấp ngoại lệ sau:Luôn nhận ngoại lệ "Loại sai tại chỉ mục hồ bơi không đổi" với 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
Tôi không biết tại sao tôi gặp lỗi này cũng như cách khắc phục.
Tôi đã đóng gói mọi thứ trong dự án Maven. Việc bố trí là như thế:
./src/test/java/soy/wimmer/CucumberIT.java
./src/test/java/soy/wimmer/CucumberStepdefs.java
./src/test/resources/cucumber/cucumber-java8.feature
./pom.xml
sự phụ thuộc tôi bao gồm trong pom.xml là:
<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>
Ngoài pom.xml chỉ tải các trình biên dịch và các plugin chạy failsafe.
định nghĩa của tôi về 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 {
}
định nghĩa tính năng của tôi:
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
Và đây là định nghĩa bước của tôi:
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();
});
}
}
Bất kỳ ý tưởng những gì tôi đang làm sai ở đây?
Vâng, thông báo ngoại lệ thiếu loại thực tế, nhưng tôi đoán đó là loại '18' và [câu trả lời này] (http://stackoverflow.com/a/23625762/2711488) được áp dụng. Tóm lại, có vẻ như Cucumber không phù hợp với Java 8, trên thực tế, thậm chí nó không tương thích 100% Java 7 sau đó… – Holger
Nó có thể là một Invokedynamic vì có các biểu thức lambda trong hàm tạo của lớp. Nhưng tôi đã cấu hình trình biên dịch thành nguồn và đích phiên bản 1.8 trong tệp pom.xml và đó là các định nghĩa bước và do đó một lớp được biên dịch cục bộ, phải không? –
Như đã nói trong câu trả lời được liên kết, vấn đề là * không * trình biên dịch.Vấn đề là có một công cụ * runtime * để xử lý tệp lớp đã biên dịch, hay còn gọi là bytecode. Nhìn vào phương thức mà ném 'IllegalArgumentException'. – Holger