使用maven的黄瓜设置

时间:2014-10-13 17:15:17

标签: java maven cucumber bdd cucumber-junit

我是Java的新手,也是黄瓜新手。我试图使用maven设置黄瓜测试框架,但在运行黄瓜测试时遇到异常。

我的pom.xml依赖项设置为:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.1.5</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.1.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>1.1.2</version>
    </dependency>
</dependencies>

要素文件包含以下条目:

package cucumber.demo;

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

public class Steps {

    @Given("^This is my first dummy step$")
    public void This_is_my_first_dummy_step() throws Throwable {
        System.out.println("Excuted the first given step.");
    }

    @When("^This is my second dummy step$")
    public void This_is_my_second_dummy_step() throws Throwable {
        System.out.println("Exceuted 2nd step.");
    }

    @Then("^This is my third dummy step$")
    public void This_is_my_third_dummy_step() throws Throwable {
        System.out.println("Excuted the third step.");
    }

}

My RunnerTest.java包含以下代码:

package cucumber.demo;

import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(
        features= {"html:target/cucumber-html-report", "json:target/cucumber-json-report.json"}
        )


public class RunnerTest {

}

当我运行应用程序时,收到以下错误消息:

Test set: cucumber.demo.RunnerTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.039 sec <<< FAILURE!
initializationError(cucumber.demo.RunnerTest)  Time elapsed: 0.005 sec  <<< ERROR!
java.lang.NoSuchMethodError: cucumber.runtime.RuntimeOptions.<init>(Ljava/util/Properties;[Ljava/lang/String;)V
    at cucumber.runtime.junit.RuntimeOptionsFactory.create(RuntimeOptionsFactory.java:32)
    at cucumber.api.junit.Cucumber.<init>(Cucumber.java:56)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:51)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

知道我收到此消息的原因。让我知道更多细节是必要的。 谢谢, Anirban

3 个答案:

答案 0 :(得分:1)

据我所知,你应该使用相同版本的3种不同的黄瓜*罐子。形成我可以告诉黄瓜-java是1.1.5而黄瓜罐子是1.1.2用于其他2.

还有更新的版本可供尝试。

http://mvnrepository.com/artifact/info.cukes/cucumber-java/1.2.2(查看底部的deps部分)

答案 1 :(得分:1)

检查maven pom.xml 黄瓜核心 黄瓜的Java 黄瓜的junit

这些文件应具有相同的版本。在您的示例中,cucumber-java artifact id的版本与其他版本不同。改变它

答案 2 :(得分:0)

也许我错了,但根据您的@Cucumber.Options注释,我想要创建两个测试报告(一个采用html格式,另一个采用json格式)。因此,您必须在注释中使用format而不是features

@RunWith(Cucumber.class)
@Cucumber.Options(
      format = {"html:target/cucumber-html-report", "json:target/cucumber-json-report.json"}
)

希望有所帮助