【问题标题】:org.testng.TestNGException: Cannot instantiate cucumber test runnerorg.testng.TestNGException:无法实例化黄瓜测试运行器
【发布时间】:2018-10-12 22:35:21
【问题描述】:

大家好,我正在尝试使用 testng 运行我的黄瓜框架,我很确定我是版本问题,但如果有人能在这里指出问题,我将不胜感激,谢谢

特点:

Feature: Application Login

Scenario: Home page default login
Given user is on landing page
When user logins into the application with username "jon" and password 
"1234"
Then Home page is displayed
And Cards displayed "true"

Scenario: Home page default login2
Given user is on landing page
When user logins into the application with username "john" and password 
"4321"
Then Home page is displayed
And Cards displayed "false"

定义:

package StepDefinations;

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



public class LoginDefination {

    @Given("^user is on landing page$")
    public void user_is_on_landing_page() throws Throwable {
        //code
        System.out.println("on landing page");
        }


    @When("^user logins into the application with username and password$")
    public void user_logins_into_the_application_with_username_and_password() throws Throwable {
        //code
        System.out.println("logging in");

    }

    @When("^user logins into the application with username \"([^\"]*)\" and password \"([^\"]*)\"$")
    public void user_logins_into_the_application_with_something_and_password_something(String strArg1, String strArg2) throws Throwable {
        System.out.println(strArg1 +" "+ strArg2);
    }

    @Then("^Home page is displayed$")
    public void home_page_is_displayed() throws Throwable {
        //code
        System.out.println("hello homepage");
    }

    @And("^Cards are displayed$")
    public void cards_are_displayed() throws Throwable {
        //code
        System.out.println("hello cards");
    }

    @And("^Cards are not displayed$")
    public void cards_are_not_displayed() throws Throwable {
        //code
        System.out.println("hello not cards");
    }

    @And("^Cards displayed \"([^\"]*)\"$")
    public void cards_displayed(String args) throws Throwable {
        //code
        System.out.println("this will or will not display the cards - " + args);
    }

}

测试运行文件:

package CucumberOptions;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

//Feature

@CucumberOptions(
        features = "src\\test\\java\\features",
        //if u want tp execute everything then just give the path till the package level
        glue= "StepDefinations"//Package name
        )
public class TestRunner extends AbstractTestNGCucumberTests {

}

测试 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="cucumber testing 101">
  <classes>
  <class name="CucumberOptions.testme"/>
  <class name="CucumberOptions.TestRunner"/>

  </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

maven 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>Cucumber</groupId>
  <artifactId>Automation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Automation</name>
  <url>http://maven.apache.org</url>


  <build>
  <resources>

 <resource>
        <directory>src/main/java/resources</directory>
        <filtering>true</filtering>
 </resource>

  </resources>
  <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.0</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>

</plugins>
  </build>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

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



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


  <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>


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




  </dependencies>
</project>

错误:

[RemoteTestNG] detected TestNG version 6.10.0
org.testng.TestNGException: 
Cannot instantiate class CucumberOptions.TestRunner
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:363)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:275)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
    at org.testng.TestClass.getInstances(TestClass.java:100)
    at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:86)
    at org.testng.TestClass.init(TestClass.java:78)
    at org.testng.TestClass.<init>(TestClass.java:41)
    at org.testng.TestRunner.initMethods(TestRunner.java:425)
    at org.testng.TestRunner.init(TestRunner.java:252)
    at org.testng.TestRunner.init(TestRunner.java:222)
    at org.testng.TestRunner.<init>(TestRunner.java:171)
    at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28)
    at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:623)
    at org.testng.SuiteRunner.init(SuiteRunner.java:189)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:136)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1375)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1355)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
    at org.testng.TestNG.runSuites(TestNG.java:1133)
    at org.testng.TestNG.run(TestNG.java:1104)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
    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.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    ... 25 more
Caused by: java.lang.NoSuchMethodError: cucumber.runtime.RuntimeOptionsFactory.<init>(Ljava/lang/Class;[Ljava/lang/Class;)V
    at cucumber.api.testng.AbstractTestNGCucumberTests.<init>(AbstractTestNGCucumberTests.java:27)
    at CucumberOptions.TestRunner.<init>(TestRunner.java:14)
    ... 30 more

它是一段简单的代码,但我只是尝试使用 testng 然后 maven 使其工作,如果有人可以帮助我,我将非常感激:)

【问题讨论】:

    标签: java maven selenium testing cucumber


    【解决方案1】:

    TestNG 和 Cucumber 库的版本不兼容。在你的依赖树中验证它们的版本并检查哪个版本的 TestNG 适合你的 Cucumber 库的版本,例如这里:

    更新:我注意到您实际上已经发布了您的pom.xml

    cucumber-testng 的版本更新为1.2.5(使用与cucumber-java 相同的版本)。使用testng 版本6.9.10(如果你不介意的话)。这样您将对齐所有库版本。

    确认:您在cucumber-testng 版本之间存在冲突。 1.1.5 和cucumber-java 版本。 1.2.5。将这两个库设置为版本1.1.5,或将它们更新为使用版本1.2.5

    【讨论】:

    • 我真是个土豆,我以为我有相同的版本,你!
    猜你喜欢
    • 2017-04-20
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    相关资源
    最近更新 更多