【问题标题】:Getting exception while running cucumber with junit / Testng使用junit / Testng运行黄瓜时出现异常
【发布时间】:2020-06-03 13:38:50
【问题描述】:

我无法同时使用 testng/Junit 运行 Cucumber TestRunner 类。我想在我的框架中包含 Testng 和 junit。我收到了一些抽象类错误。我的项目中有所有依赖项。我这里有 2qns。 1.我可以在我的黄瓜脚本中使用Testng注释,因为RunWith是从黄瓜junit导入的 2.如何解决错误执行我的testRun。

TestRunner 类

package testRun;


import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import io.cucumber.testng.AbstractTestNGCucumberTests;

@RunWith(Cucumber.class)
@CucumberOptions(features="/OpenCart/src/test/resources/testFeature",glue= {"stepDefinitions"},
plugin = { "pretty", "html:target/cucumber-reports" })

public class TestRunner extends AbstractTestNGCucumberTests{

}

错误日志

java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class io.cucumber.junit.CucumberOptions.objectFactory()
    at java.lang.reflect.Method.getDefaultValue(Method.java:612)
    at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:132)
    at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:266)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
    at java.lang.Class.createAnnotationData(Class.java:3521)
    at java.lang.Class.annotationData(Class.java:3510)
    at java.lang.Class.getAnnotation(Class.java:3415)
    at org.junit.internal.builders.IgnoredBuilder.runnerForClass(IgnoredBuilder.java:10)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
    at org.junit.internal.requests.ClassRequest.createRunner(ClassRequest.java:28)
    at org.junit.internal.requests.MemoizingRequest.getRunner(MemoizingRequest.java:19)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

文件夹结构

依赖关系

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>5.5.0</version>
</dependency>
  
  <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>5.7.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>


<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-core</artifactId>
    <version>5.5.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.cucumber/gherkin -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>gherkin</artifactId>
    <version>13.0.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>5.5.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>5.5.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm-deps -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-jvm-deps</artifactId>
    <version>1.0.6</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.13.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>4.1.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>


<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>3.7.1</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.10.19</version>
    <scope>test</scope>
</dependency>

我是否遗漏了什么...如果我遗漏了什么,请帮我解决这个问题。

【问题讨论】:

  • 为什么你的项目中同时需要 junit 和 testng?

标签: java selenium junit cucumber testng


【解决方案1】:

如果您希望 Junit 和 Testng 在同一个项目中,那么最好创建两个不同的运行器,因为使用 junit 运行扩展 AbstractTestNGCucumberTests 的运行器文件会导致该问题。从我的角度来看,您可以创建两个不同的运行程序文件,然后在 maven pom.xml 中,您可以为 Junit 和 Testng 创建不同的配置文件

【讨论】:

  • 我明白了你的意思,但我什至无法使用 junit 运行 ..不管 Testng .. 我得到了上述异常
【解决方案2】:

关于缺少方法的错误,或者在它们所在的地方不允许使用的方法通常是由依赖项不匹配引起的。因此,如果您查看您的 pom,您会发现您使用的是不同版本的 Cucumber。此外,您还包括您并不严格需要的传递依赖项。

io.cucumber 你应该只需要cucumber-javacucumber-junitcucumber-testng。所有其他依赖项都是可传递的,并且会被 maven 自动包含在正确的版本中。

我还建议尝试让 junit 或 testng 工作。试图让两者同时工作只会让你更复杂。

【讨论】:

  • 看起来你的观点是有效的。我会试试这个,并会更新你..
  • 我试图通过评论 cucumber-testng 来运行,但我仍然遇到同样的错误。
  • 我建议你尝试多种方法。通过删除cucumber-testng,你只做了一个。并且由于您使用的是 Eclipse,所以不要忘记在对 pom 进行更改后重新导入/刷新您的项目。
  • 该问题已解决,是的,您的建议对我有用.. 你能看看这个问题吗.. stackoverflow.com/questions/62189075/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
相关资源
最近更新 更多