首页 > 解决方案 > 使用 Cucumber 和 Serenity 运行 springboot 时出错

问题描述

我正在尝试使用 serenity-cucumber 测试我的 Spring Boot 应用程序。

为此,我有我的切入点:

package integration.com.foo.proj;

import io.cucumber.junit.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(features = "src/test/resources")
public class CucumberIntegrationTest {
}

然后我的 Baseclass 将由 step defs 使用,

package integration.com.foo.proj;

import com.foo.proj.Application;
import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@CucumberContextConfiguration
@SpringBootTest(classes = Application.class)
@ActiveProfiles("sit")
public class SpringIntegrationTest {


}

而且,我的步骤定义之一是在这一点上运行一些虚拟测试以充当胶水。

package integration.com.foo.proj;

import io.cucumber.java.en.And;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Value;

public class StepDefs extends SpringIntegrationTest {

  @Value("${config.value}")
  private String configValue;

  @When("^the client calls /version$")
  public void the_client_issues_GET_version() throws Throwable {
    String s = configValue;
  }
  @Then("^the client receives status code of (\\d+)$")
  public void the_client_receives_status_code_of(int statusCode) throws Throwable {
    String s = configValue;
  }
  @And("^the client receives server version (.+)$")
  public void the_client_receives_server_version_body(String version) throws Throwable {
    String s = configValue;
  }
}

我的特征类:

Feature: the version can be retrieved
        Scenario: client makes call to GET /version
            When the client calls /version
            Then the client receives status code of 200
            And the client receives server version 1.0

我已经导入了以下依赖项,(也尝试过使用 testCompile):

testImplementation 'io.cucumber:cucumber-java:6.10.4'
testImplementation 'io.cucumber:cucumber-junit:6.10.4'
testImplementation 'io.cucumber:cucumber-spring:6.10.4'

testImplementation 'net.serenity-bdd:serenity-core:2.4.49'
testImplementation 'net.serenity-bdd:serenity-spring:2.4.49'
testImplementation 'net.serenity-bdd:serenity-junit:2.4.49'
testImplementation 'net.serenity-bdd:serenity-cucumber5:2.2.6'

当我运行测试时,出现以下错误。

Exception in thread "main" java.lang.NoSuchMethodError: io.cucumber.core.options.CommandlineOptionsParser: method <init>()V not found
    at net.serenitybdd.cucumber.cli.Main.run(Main.java:24)
    at net.serenitybdd.cucumber.cli.Main.main(Main.java:19)

在最初的搜索过程中,遇到了这篇提到要导入的帖子cucumber-java,并且cucumber-junit在其中一个答案中,但不起作用。

这是因为依赖吗?

我也关注了 serenity-bdd 的帖子,但无法使其正常工作。
开始使用 Cucumber 和 Serenity
集成测试 Spring

标签: javaspring-bootcucumberserenity-bddcucumber-serenity

解决方案


我通常不会回答我自己的问题,但我在 SO 上看到的类似问题很少。

就我而言,删除解决了这个问题。
testCompile ("io.cucumber:cucumber-spring:6.10.4")

但是,要获得正确版本的依赖项,如果您遇到此类问题,请参考 github 并检查发行说明和依赖项 tee。

就我而言,问题出在Cucumber-Serenity5上。

因此,从 build.gradle 中获取依赖项是有效的。 https://github.com/serenity-bdd/serenity-cucumber5/blob/master/build.gradle

请注意,实际版本在 gradle.proporties 中定义。
https://github.com/serenity-bdd/serenity-cucumber5/blob/master/gradle.properties


推荐阅读