简体   繁体   English

在junit中运行黄瓜时出错

[英]Error while running cucumber in junit

Hi i am new to cucumber java.嗨,我是黄瓜 Java 的新手。 i am trying to run a simple cucumber feature test.我正在尝试运行一个简单的黄瓜功能测试。 below are my feature file, step definition file and junit runner file.下面是我的特征文件、步骤定义文件和 junit 运行文件。 but i am not able to run the test succesfully in cucumber-java,cucumber-junit 1.1.6 version.但我无法在cucumber-java,cucumber-junit 1.1.6 版本中成功运行测试。

Feature file特征文件

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 file步骤定义文件

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

Cucumber runner file黄瓜赛跑档案

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 {

}

I am getting below error:我收到以下错误:

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 POM文件

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

But when i am running with 1.0.14 version of the same i am able to run it successfully.但是当我使用相同的 1.0.14 版本运行时,我能够成功运行它。 whys is it so?为什么会这样? does 1.1.6 version has some bug in it to run? 1.1.6 版本是否有一些错误可以运行? TIA!!!蒂亚!!!

In Simple term, We need same jar version for cucumber-core,cucumber-java,cucumber-junit.简单来说,我们需要为cucumber-core、cucumber-java、cucumber-junit 使用相同的jar 版本。 To overcome this issue :)为了克服这个问题:)

Error, we are getting because of the jar files and corresponding version(not suitable for our JDK).错误,我们是因为 jar 文件和相应版本(不适合我们的 JDK)而得到的。 I played with jar file - now issue is resolved.我玩过 jar 文件 - 现在问题解决了。 And also able to run and generate output.并且还能够运行和生成输出。

My JDK version- 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我的JDK版本- 1.8.0_60 JARS,junit -4.12黄瓜-java-1.2.2.jar黄瓜-junit-1.2.2.jar黄瓜-核心-1.2.2.jar小黄瓜-2.12.2.jar黄瓜-jvm- deps-1.0.3.jar

Note: java,junit,core should be in same version.注意:java,junit,core 应该是同一个版本。 Remove unwanted cucumber jars.取出不需要的黄瓜罐。 Debug by install only two(Java and Junit) jar files(different version) and try to resolve NoSuchMethod error.通过仅安装两个(Java 和 Junit)jar 文件(不同版本)进行调试并尝试解决 NoSuchMethod 错误。

The error means that cucumber.runtime.RuntimeOptions has no constructor which takes a List as argument.该错误意味着cucumber.runtime.RuntimeOptions没有将List作为参数的构造函数。

Since all classes are part of cucumber, I suspect a bug in the release.由于所有类都是黄瓜的一部分,我怀疑发布中存在错误。 Run mvn dependency:tree and search the output for cucumber .运行mvn dependency:tree并在输出中搜索cucumber Make sure that you have only a single version of the dependency.确保您只有一个版本的依赖项。

If your classpath is good, try an older version.如果您的类路径不错,请尝试使用旧版本。

If that works, download the sources for cucumber and compile them.如果可行,请下载 Cucumber 的源代码并编译它们。 Does it work now?现在有效吗? If so, open a bug report telling the Cucumber project that the latest release wasn't compile correctly.如果是这样,打开一个错误报告,告诉 Cucumber 项目最新版本没有正确编译。

I'm new and still learning.我是新手,还在学习。 I had a similar 'cucumber runner initialization error' message:我有一个类似的“黄瓜运行器初始化错误”消息:

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 <= My Jars when I was having an error.出现错误时,引用的库<= 我的罐子。 I then replaced my cucumber-junit-1.1.5.jar with cucumber-junit-1.2.2.jar and the error message is now resolved.然后我用cucumber-junit-1.2.2.jar 替换了我的cucumber-junit-1.1.5.jar,错误信息现在解决了。 I can now run my cucumber junit test.我现在可以运行我的黄瓜 junit 测试了。 I started with the latest jars but they seems to have issues.我从最新的罐子开始,但它们似乎有问题。 I then tried several different combination.然后我尝试了几种不同的组合。 I read 'rule of thumb' (also mentioned above) that the three jars (-core, -java and -junit) need to be of same version.我读过“经验法则”(上面也提到过),三个 jar(-core、-java 和 -junit)需要具有相同的版本。 Only then I was able to run my feature files and junit tests.只有这样我才能运行我的功能文件和 junit 测试。

My current working jars are:我目前的工作罐子是:

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

I hope this helps.我希望这有帮助。

Check your dependencies: Cucumber dependencies should be all aligned:检查您的依赖项:Cucumber 依赖项应该全部对齐:

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

我解决了类似的问题,保持cucumber-junitcucumber-java相同。

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小黄瓜-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

this worked for me and No Such method found error get resolved这对我有用,没有发现这种方法错误得到解决

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

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