使用testNG Maven从info.cukes更改为io.cucumber。抛出运行时错误

时间:2018-11-07 02:40:50

标签: java maven selenium cucumber testng

我有一个使用maven的硒黄瓜testng框架。当我用testng运行黄瓜测试时,我希望获得步骤说明。这是一个旧项目,因此我将依赖项从info.cukes更改为最新版本的io.cucumber。但是,每当我运行运行器测试文件时,它似乎都会抛出错误。

java.lang.NoSuchMethodError: cucumber.runtime.RuntimeOptions.getPluginFormatterNames()Ljava/util/List;

    at cucumber.runtime.formatter.Plugins.createPlugins(Plugins.java:64)
    at cucumber.runtime.formatter.Plugins.<init>(Plugins.java:37)
    at cucumber.api.testng.TestNGCucumberRunner.<init>(TestNGCucumberRunner.java:56)
    at cucumber.api.testng.AbstractTestNGCucumberTests.setUpClass(AbstractTestNGCucumberTests.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:458)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:168)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:105)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

我具有以下Maven依赖项,但不知道是否缺少要运行测试的依赖项?任何帮助将不胜感激。当我搜索此问题时,似乎大多数情况下都可以通过与我执行的所有io.cucumber文件具有相同的版本依赖性来解决。

 <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.2.0</version>
    <scope>test</scope>
</dependency>

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

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

</dependency>

        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.21.0</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/tag-expressions -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>tag-expressions</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-expressions -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-expressions</artifactId>
            <version>6.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/messages -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>messages</artifactId>
            <version>2.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java8 -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java8</artifactId>
            <version>4.2.0</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/com.github.mkolisnyk/cucumber-report-generator -->
<dependency>
    <groupId>com.github.mkolisnyk</groupId>
    <artifactId>cucumber-report-generator</artifactId>
    <version>1.3</version>
</dependency>



    <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>  

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

        <!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
<!--<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>gherkin</artifactId>
    <version>2.12.2</version>
    <scope>provided</scope>
</dependency>-->
        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-core</artifactId>
    <version>4.2.0</version>
</dependency>



     <!-- https://mvnrepository.com/artifact/ru.sbtqa.tag/cucumber-runner -->
<dependency>
    <groupId>ru.sbtqa.tag</groupId>
    <artifactId>cucumber-runner</artifactId>
    <version>1.0.7</version>
</dependency>

2 个答案:

答案 0 :(得分:1)

检查maven pom.xml黄瓜核心黄瓜-java黄瓜-junit

这些文件应该具有相同的版本。在您的示例中,cucumber-java工件ID的版本与其他版本不同。

尝试更改它。

答案 1 :(得分:1)

在下面的一些代码片段中查找以解决问题

  1. 检查是否有一个transitive dependency来对info.cukes进行分组ID

    $ mvn dependency:tree | grep 'info.cuke'
    [INFO] |  +- info.cukes:cucumber-junit:jar:1.2.5:compile
    [INFO] |  |  \- info.cukes:cucumber-core:jar:1.2.5:compile
    [INFO] |  |     +- info.cukes:cucumber-html:jar:0.2.3:compile
    [INFO] |  |     \- info.cukes:cucumber-jvm-deps:jar:1.0.5:compile
    [INFO] |  +- info.cukes:cucumber-testng:jar:1.2.5:compile
    [INFO]    |  +- info.cukes:cucumber-java:jar:1.2.5:compile
    [INFO]    |  +- info.cukes:gherkin:jar:2.12.2:compile
    

    在检查完整输出时会发现您的依赖项取决于它

    ...
    [INFO] +- com.github.mkolisnyk:cucumber-report-generator:jar:1.3:compile
    ...
    [INFO] |  +- info.cukes:cucumber-junit:jar:1.2.5:compile
    ...
    [INFO] |  +- info.cukes:cucumber-testng:jar:1.2.5:compile
    ...
    [INFO] \- ru.sbtqa.tag:cucumber-runner:jar:1.0.7:compile
    [INFO] \- ru.sbtqa.tag:cucumber-runner:jar:1.0.7:compile
    [INFO]    +- ru.yandex.qatools.allure:allure-cucumber-jvm-adaptor:jar:1.6.4:compile
    ...
    [INFO]    |  +- info.cukes:cucumber-core:jar:1.2.5:compile
    ...
    [INFO]    |  +- info.cukes:cucumber-java:jar:1.2.5:compile
    [INFO]    |  +- info.cukes:gherkin:jar:2.12.2:compile
    ...
    [INFO]    +- info.cukes:cucumber-junit:jar:1.2.5:compile
    
  2. ,因为具有相同限定名称的类可能会重叠,因此请排除pom.xml

    中的那些瞬时依赖项
        <dependency>
            <groupId>com.github.mkolisnyk</groupId>
            <artifactId>cucumber-report-generator</artifactId>
            <version>1.3</version>
            <exclusions>
                <exclusion>
                    <groupId>info.cukes</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
  3. 再次运行测试会引发异常

    java.lang.NoClassDefFoundError: gherkin/IGherkinDialectProvider
    
  4. 当我们显式定义依赖项io.cucumber:gherkin时,它可能与指定的<version>相关,将其注释掉以查看是否获取了另一个版本

    <!--<dependency>-->
        <!--<groupId>io.cucumber</groupId>-->
        <!--<artifactId>gherkin</artifactId>-->
        <!--<version>6.0.14</version>-->
    <!--</dependency>-->
    

    这表明io.cucumber:cucumber-core:jar:4.2.0取决于5.1.0的版本io.cucumber:gherkin

    $ mvn dependency:tree
    ...
    [INFO] +- io.cucumber:cucumber-core:jar:4.2.0:compile
    [INFO] |  +- io.cucumber:cucumber-html:jar:0.2.7:compile
    [INFO] |  +- io.cucumber:gherkin:jar:5.1.0:compile
    

有了这些更改,至少TestNG单元测试正在运行。

由于依赖项com.github.mkolisnyk:cucumber-report-generatorru.sbtqa.tag:cucumber-runner取决于info.cuke依赖项,它们可能不再正常工作。要解决此类问题,您可以:

  • 检查较新的版本,该版本可能不取决于组ID info.cuke
  • 使用早期版本的io.cucumber依赖项
  • info.cuke依赖项进行更细粒度的排除,而不是排除所有工件ID
  • 以上几点的组合
  • 其他情况,取决于您的用例和要求

也许这些步骤可以帮助您找到适合您的解决方案。