【问题标题】:Project doesn't recognize cucumber-picocontainer dependency项目无法识别 cucumber-picocontainer 依赖项
【发布时间】:2016-03-04 02:47:50
【问题描述】:

我目前正在使用 cucumber、JUnit 和 Selenium 开发一个 java 测试框架。我已经参与过类似的项目,但我在这个项目上遇到了问题。

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

这是我的配置示例:

  • 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>
    

TestContext.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 来做同样的事情并且它正在工作。

【问题讨论】:

  • 我遇到了类似的问题,你有没有找到解决这个问题的方法?
  • @himawan_r 不,我的问题当时没有解决。从那以后我改变了很多东西。而且我不再使用 picocontainer
  • 我遵循 Vacha Dave 的解决方案,使所有 info cukes 版本相同,它适用于我 :)

标签: java maven selenium cucumber picocontainer


【解决方案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>

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

          <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>
      

      【讨论】:

        【解决方案4】:

        我遇到了类似的问题。

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

        【讨论】:

        • 这不是问题的答案。如果您在社区中找不到答案,您应该询问新的。
        • 感谢@VachaDave,刚刚尝试了您的答案,它成功了。这应该是一个公认的答案。
        • 我无法真正将其标记为已接受的答案。正如您在我的 pom.xml 示例中看到的那样,我的所有黄瓜依赖项的版本都由单个参数管理 --> 因此,相同的版本
        • @NicolasG.Duvivier 好吧 np,只是想让其他可能发现此问题的人尝试一下。问题可能很棘手
        • @himawan_r 因为它为其他人解决了类似的问题,我什至无法再访问此代码。我接受了你的回答
        【解决方案5】:

        不包含

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

        除了 cucumber-junit 和 cucumber-picocontainer。

        那是我项目的问题。

        【讨论】:

          【解决方案6】:

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

          <dependency>   
              <groupId>org.picocontainer</groupId>  
              <artifactId>picocontainer</artifactId>
              <version>2.14.3</version>
          </dependency>
          

          【讨论】:

          • 感谢您的回答。我会尝试的,但我想我已经尝试过了。我会让你知道结果。
          猜你喜欢
          • 1970-01-01
          • 2023-01-18
          • 2021-04-16
          • 2016-11-05
          • 2013-08-21
          • 2021-01-11
          • 1970-01-01
          • 1970-01-01
          • 2020-08-30
          相关资源
          最近更新 更多