在junit中运行黄瓜时出错

时间:2014-05-09 07:49:26

标签: java cucumber cucumber-junit

你好我是黄瓜java的新手。我正在尝试运行简单的黄瓜功能测试。下面是我的功能文件,步骤定义文件和junit runner文件。但是我无法在cucumber-java,cucumber-junit 1.1.6版本中成功运行测试。

功能文件

Feature: Test if f1 feature is working
Scenario: valid scenario
Given input1 is "t"
When input2 is also "t"
Then result should be "pass"

Stepdefinition文件

package cucumberFrameworkPractise;

import org.junit.Assert;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;



public class CucumberStepDefinitionTest {
    String input1,input2,result;
@Given("input1 is \"([^\"]*)\"$")
public void input1(String input1)
{
    this.input1=input1;
}
@When("input2 is also \"([^\"]*)\"$")
public void input2(String input2)
{
    this.input2=input2;
}
@Then("result should be \"([^\"]*)\"$")
public void result(String result)
{
    this.result=result;
    Assert.fail();
}
}

黄瓜跑步者档案

package cucumberFrameworkPractise;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/cucumberFrameworkPractise", format = {
        "pretty", "html:target/cucumber-htmlreport",
"json-pretty:target/cucumber-report.json" })
public class CucumberRunner {

}

我收到以下错误:

java.lang.NoSuchMethodError: cucumber.runtime.RuntimeOptions.<init>(Ljava/util/List;)V
    at cucumber.runtime.RuntimeOptionsFactory.create(RuntimeOptionsFactory.java:24)
    at cucumber.api.junit.Cucumber.<init>(Cucumber.java:58)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>mrunal</groupId>
  <artifactId>cucumbertest</artifactId>
  <version>1.0</version>
  <build>
  <plugins>
  <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.1</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
  </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.0.14</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.0.14</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>1.1.5</version>
    </dependency>
  </dependencies>
</project>

但是当我运行相同的1.0.14版本时,我能够成功运行它。这是为什么呢? 1.1.6版本有没有运行的错误? TIA !!!

6 个答案:

答案 0 :(得分:6)

错误意味着cucumber.runtime.RuntimeOptions没有以List为参数的构造函数。

由于所有课程都是黄瓜的一部分,我怀疑该版本中有一个错误。运行mvn dependency:tree并搜索cucumber的输出。确保您只有一个版本的依赖项。

如果您的类路径很好,请尝试旧版本。

如果有效,请下载黄瓜的来源并进行编译。它现在有效吗?如果是这样,打开一个错误报告告诉Cucumber项目最新版本没有正确编译。

答案 1 :(得分:4)

简单来说,我们需要相同的jar版黄瓜核心,黄瓜-java,黄瓜-junit。要克服这个问题:)

错误,我们因为jar文件和相应的版本(不适合我们的JDK)而得到。我玩jar文件 - 现在问题解决了。并且还能够运行并生成输出。

我的JDK版本 - 1.8.0_60 JARS,junit -4.12 cucumber-java-1.2.2.jar cucumber-junit-1.2.2.jar cucumber-core-1.2.2.jar gherkin-2.12.2.jar cucumber -jvm-DEPS-1.0.3.jar

注意:java,junit,core应该在同一个版本中。去掉不需要的黄瓜罐子。调试只安装两个(Java和Junit)jar文件(不同版本)并尝试解决NoSuchMethod错误。

答案 2 :(得分:0)

我是新手,还在学习。我收到了类似的“黄瓜跑腿初始化错误”消息:

java.lang.NoSuchMethodError: cucumber.runtime.RuntimeOptionsFactory.<init>(Ljava/lang/Class;[Ljava/lang/Class;)V
    at cucumber.api.junit.Cucumber.<init>(Cucumber.java:59)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:87)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:73)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:46)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:522)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

Referenced Libraries <=我遇到错误时的罐子。然后,我用黄瓜-junit-1.2.2.jar替换了我的黄瓜-junit-1.1.5.jar,错误消息现在已解决。我现在可以运行黄瓜junit测试。 我从最新的罐子开始,但是它们似乎有问题。然后,我尝试了几种不同的组合。我读了“经验法则”(在上文中也提到过),这三个jar(-core,-java和-junit)必须具有相同的版本。只有这样,我才能运行功能文件和junit测试。

我当前的罐子是:

gherkin-2.12.2
cucumber-core-1.2.2
cucumber-java-1.2.2
cucumber-junit-1.2.2
cucumber-html-0.2.3
cucumber-jvm-deps-1.0.3
hamcrest-all-1.3
junit-4.11
selenium-server-standalone-3.13.0

我希望这会有所帮助。

答案 3 :(得分:0)

检查依赖项:黄瓜依赖项应全部对齐:

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm --> 
<dependency>
   <groupId>info.cukes</groupId>
   <artifactId>cucumber-jvm</artifactId>
   <version>1.2.2</version>
   <type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java --> 
<dependency>
   <groupId>info.cukes</groupId>
   <artifactId>cucumber-java</artifactId>
   <version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core --> 
<dependency>
   <groupId>info.cukes</groupId>
   <artifactId>cucumber-core</artifactId>
   <version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit --> 
<dependency>
   <groupId>info.cukes</groupId>
   <artifactId>cucumber-junit</artifactId>
   <version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm-deps --> 
<dependency>
   <groupId>info.cukes</groupId>
   <artifactId>cucumber-jvm-deps</artifactId>
   <version>1.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit --> 
<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.11</version>
</dependency>

答案 4 :(得分:0)

我通过保持cucumber-junitcucumber-java的版本相同来解决了类似的问题。

答案 5 :(得分:0)

gherkin-2.12.2 黄瓜芯1.2.2 黄瓜java-1.2.2 黄瓜-junit-1.2.2 黄瓜-html-0.2.3 黄瓜-jvm-deps-1.0.3 hamcrest-all-1.3 junit-4.11 selenium-server-standalone-3.13.0

这对我有用,没有这样的方法发现错误得到解决