当前位置: 首页 > 知识库问答 >
问题:

使用junit/Testng运行cucumber时获得异常

黄宏大
2023-03-14

我无法同时运行TestNG/JUnit的Cucumber TestRunner类。我希望在我的框架中包含Testng和junit。我得到一些abc类错误。我的项目中有所有的依赖项。我这里有2qns。1.当runwith从cucumber junit导入时,我可以在我的cucumber脚本中使用Testng注释吗?2.如何解决错误执行我的testrun。

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>

我错过了什么...如果我错过了什么,请帮我把它修好。

共有1个答案

陶胤运
2023-03-14

有关缺少方法或方法所在位置不允许的错误通常是由依赖项不匹配引起的。所以,如果你看看你的pom,你会发现你使用的是不同版本的黄瓜。此外,您还包含了您并不严格需要的可传递依赖项。

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

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

 类似资料:
  • 下面是我的pom.xml、testng.xml文件和TestRunners。 下面是我的pom.xml文件 [Utils][ERROR][ERROR]java.lang.NullPointerException在Cucumber.api.testng.AbstractTestngCucumberTests.Scenaries(AbstractTestngCucumberTests.java:31)

  • 我试图用JUnit运行一个cucumber测试,但我得到了异常。 JUnit代码: 例外情况: 设置cucumber测试时的NoClassDefoundErr 线程“main”java.lang.NoClassDeffounderRror:gherkin/formatter/formatter 以下问题的答案已经建议添加相同版本的黄瓜jar文件。但是,由于Maven存储库中可用的jar文件本身没有

  • 我对cucumber相当陌生。当我注意到运行单个特性与运行整个套件(从IntelliJ)时的差异时,我只是通过创建几个测试特性来进行试验。 我注意到,当我运行单个特性时,它使用cucumber-jvm选项运行,在本例中,CucumberConfig(定义runner和cucumber选项的空白类)和runner没有被使用。但是,当我运行整个套件时,它作为JUnit测试运行,显然,在本例中,Conf

  • 我有两节cucumber赛跑课。其中一个是基于JUnit的runner,另一个是基于TestNG的。Junit one是我为API测试实现的第一个运行程序,并通过maven按预期运行。我在项目中添加了一个web测试,并使用TestNG来帮助我实现跨浏览器测试。因此,我创建了一个额外的cucumber runner(基于TestNG)。当我通过< code>testng.xml文件运行web测试时,

  • 使用: cucumber芯-1.2.4 cucumber-java-1.2.4 cucumber-junit-1.2.4 Junit-4.12 Eclipse Mars.1 Java 8