5

我目前正在使用黄瓜、JUnit 和 Selenium 开发一个 java 测试框架。我已经从事过这样的项目,但我在这个项目上遇到了问题。

我正在尝试创建一个单例的 Context 类。我想使用 cucumber-picocontainer 让这个类在每个步骤定义类中都可以访问。我在 pom.xml 中添加了依赖项,但每次尝试执行测试时,都会出现异常:“NewLoginSteps 没有空构造函数。如果需要 DI,请将 cucumber-picocontainer 放在类路径中” . 我尝试手动导入罐子,但没有帮助。

这是我的配置示例:

  • pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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>
    
    
        <properties>
            <cucumber.version>1.2.4</cucumber.version>
            <selenium-java.version>2.39.0</selenium-java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-java</artifactId>
                <version>${cucumber.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-picocontainer</artifactId>
                <version>${cucumber.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-junit</artifactId>
                <version>${cucumber.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
    </project>
    

测试上下文.java:

public class TestContext {
    private static Map<String, String> siteLocations = new HashMap<String, String>();
    private static boolean initialized = false;
    private static boolean firstInitDone = false;
    private static WebDriver driver;
    private static boolean testsToRun = false;
    private static AutomatedTestMode modeAsEnum;

    @Before
    public void setUp(Scenario scenario) {
        initialize();
        Log.startTestCase(scenario.getName());
        afterAll();
    }
    ....
}

一个步骤定义类:

public class NewLoginSteps extends NewSuperSteps {

    public NewLoginSteps(TestContext context){
        super(context);
    }


    @When("^I log in nova as \"([^\"]*)\" user with \"([^\"]*)\" \"([^\"]*)\"$")
    public void newLogin(String instance, String username, String password){
        Assert.assertEquals(true, false);

    }

    @Then("^The user is connected$")
    public void The_user_is_connected(){
        throw new PendingException();
    }

}

我的 superSepts 类:

public class NewSuperSteps {
    protected TestContext context;
    public NewSuperSteps(TestContext context){
        this.context=context;
    }

}

你知道我做错了什么吗?我已经使用 picocontainer 来做同样的事情并且它正在工作。

4

6 回答 6

9

我遇到了类似的问题。

问题出在 info cukes 的版本中。您的 pom.xml 中需要所有 cucumber-* 的相同版本。这为我解决了这个问题。

于 2017-01-13T05:03:25.833 回答
1

将以下依赖项添加到您的 pom.xml

<dependency>   
    <groupId>org.picocontainer</groupId>  
    <artifactId>picocontainer</artifactId>
    <version>2.14.3</version>
</dependency>
于 2015-12-15T16:39:57.917 回答
1

添加以下依赖项对我有用,

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
于 2019-03-27T23:54:33.230 回答
1

info.cukes 工件现在移至 io.cucumber。每个与黄瓜相关的依赖项都应该具有相同的版本号。

<properties>
    <io.cucumber.version>4.7.2</io.cucumber.version>
</properties>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>${io.cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>${io.cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- Using PicoContainer to share state between steps in a scenario -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>${io.cucumber.version}</version>
        <scope>test</scope>
    </dependency>
于 2020-11-04T10:51:43.350 回答
0

不包括

<dependency>   
    <groupId>info.cukes</groupId>  
    <artifactId>cucumber-java</artifactId>
    <version>1.2.2</version>
    <scope>test</scope>
</dependency>

除了 cucumber-junit 和 cucumber-picocontainer。

那是我项目的问题。

于 2016-01-13T11:23:27.540 回答
0

对我来说,即使在您的 pom.xml 中提供相同版本的所有 cucumber-* 之后,问题仍然存在。我下载了所有依赖 jar 并添加到依赖选项卡下的模块设置中。现在它工作正常。

于 2020-03-13T06:39:24.273 回答