Cucumber+Spring Boot:通过 gradle cucumber Cli 检索 application.properties 变量和 运行 项目

Cucumber+SpringBoot: Retreive application.properties variables and run project via gradle cucumberCli

我整天都在尝试解决这个问题,但是没能成功。 那么让我们来描述一下: 我的意图: 在SpringBoot应用中,在src/resources/application.properties中,我定义了两个变量:

myDefaultBrowser=https://www.https://www.mozilla.org/en-US/
mySecondBrowser=https://www.google.com/chrome/

有一个选项可以使用第三个变量,但该变量要从环境中获取。

我做了什么:

创建了gradle springboot 项目。这是结构: ProjectStructure

我有一些 youtube 视频,谷歌搜索来创建这样的结构。

功能文件是:Chrome 和 Firefox

正在显示 firefox.feature

Feature: Test to see appproperties variable

  Scenario: Get and show firefox browser
    When firefox variable is stored into appprop show it

firefox 的步骤定义:(类似 chrome)

public class FirefoxSD {

    @Autowired
    RunConfiguration runConfiguration;

    String fireFoxURI;

    @Before
    public void setUp() {
        this.fireFoxURI = runConfiguration.getDefaultBrowser();
    }

    @When("firefox variable is stored into appprop show it")
    public void showFirefoxURI() {
        baseURI = fireFoxURI;
        System.out.println("***************************************************************");
        System.out.println("This is firefox URI from application properties: " + baseURI);
        System.out.println("***************************************************************");
    }
}

然后我创建了 运行ner class, firefox:

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources/featureFiles/firefoxFF",
        glue ={"com/cucumbertest/cucumber/stepDefinitions/firefoxSD","com/cucumbertest/cucumber/cucumberConfig"},
        plugin = { "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"},
        dryRun = false
)

public class FireFoxRunnerTest {
}

另外三个 classes: 配置加载:

@Component
@PropertySource({"classpath:application.properties"})
public class ConfigLoad {
}

运行配置:

@Component
public class RunConfiguration {

    @Value("${myDefaultBrowser}")
    private String defaultBrowser;

    @Value("${mySecondBrowser}")
    private String secondBrowser;

    public RunConfiguration() {
    }

    public String getDefaultBrowser() {
        return defaultBrowser;
    }

    public void setDefaultBrowser(String defaultBrowser) {
        this.defaultBrowser = defaultBrowser;
    }

    public String getSecondBrowser() {
        return secondBrowser;
    }

    public void setSecondBrowser(String secondBrowser) {
        this.secondBrowser = secondBrowser;
    }
}

黄瓜配置:

CucumberContextConfiguration
@SpringBootTest(classes = CucumberApplication.class)
public class CucumberConfig {

}

build.gradle:

import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
    id 'org.springframework.boot' version '2.6.7'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.cucumbertest'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    testImplementation group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
    testImplementation group: 'io.rest-assured', name: 'rest-assured', version: '4.5.1'
    implementation group: 'io.cucumber', name: 'cucumber-java', version: '7.2.3'
    testImplementation group: 'io.cucumber', name: 'cucumber-junit', version: '7.2.3'
    implementation group: 'io.cucumber', name: 'cucumber-spring', version: '7.2.3'


    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.8.2'
    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.8.2'
    testImplementation group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '5.8.2'

    implementation group: 'tech.grasshopper', name: 'extentreports-cucumber7-adapter', version: '1.3.0'
    implementation group: 'io.rest-assured', name: 'xml-path', version: '4.5.1'
    implementation group: 'io.rest-assured', name: 'json-path', version: '4.5.1'
}

// cucumber

configurations {
    cucumberRuntime {
        extendsFrom testImplementation
    }
}

task cucumberCli() {
    dependsOn assemble, testClasses
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = [
                    '--plugin', 'pretty',
                    '--plugin', 'com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:',
                    '--glue', 'com/digitalautomationsolution/dascucumber/stepDefinitions',
                    'src/test/resources/featureFiles'
            ]
        }
    }
}

test {
    useJUnitPlatform()

    testLogging {
        events TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED
    }

    systemProperties(project.gradle.startParameter.systemPropertiesArgs)
}

当我 运行 ChromeRunnerTest 和 FirefoxRunnerTest 时,第一个然后其他,测试 运行 就好了,但是当我 运行 通过 [=75= 测试时] 与:gradle cucumberCli 我收到此错误:

SEVERE: Exception while executing pickle
java.util.concurrent.ExecutionException: io.cucumber.core.backend.CucumberBackendException: Please annotate a glue class with some context configuration.

For example:

   @CucumberContextConfiguration
   @SpringBootTest(classes = TestConfig.class)
   public class CucumberSpringConfiguration { }
Or: 

   @CucumberContextConfiguration
   @ContextConfiguration( ... )
   public class CucumberSpringConfiguration { }
    at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:191)
    at io.cucumber.core.runtime.Runtime.runFeatures(Runtime.java:117)
    at io.cucumber.core.runtime.Runtime.lambda$run[=21=](Runtime.java:82)
    at io.cucumber.core.runtime.Runtime.execute(Runtime.java:94)
    at io.cucumber.core.runtime.Runtime.run(Runtime.java:80)
    at io.cucumber.core.cli.Main.run(Main.java:87)
    at io.cucumber.core.cli.Main.main(Main.java:30)
Caused by: io.cucumber.core.backend.CucumberBackendException: Please annotate a glue class with some context configuration.

我尝试用@SpringBootTest(也有 stepDef classes)注释 classes,但收到此错误。

这是 git 上的代码:Github code

我做错了什么或者我在实施中遗漏了什么?

此致, 约万.

Cucumber 使用 @CucumberContextConfiguration 注释 class 来确定使用哪个 class 来配置 spring 应用程序上下文。

这个 class 必须在粘合路径上。

FireFoxRunnerTest 中你有两个这样的路径:

glue ={
  "com/cucumbertest/cucumber/stepDefinitions/firefoxSD",
  "com/cucumbertest/cucumber/cucumberConfig"
},

但是在任务中 cucumberCLI 你 运行 黄瓜有一个单一的胶水参数:

'--glue', 'com/digitalautomationsolution/dascucumber/stepDefinitions',

仔细观察差异。

其他人的解决方案: 在我的 firefox 运行ner(和 chrome 运行ner)中,我有这些行:

    @RunWith(Cucumber.class)
    @CucumberOptions(
            features = "src/test/resources/featureFiles/firefoxFF",
            glue ={"com/cucumbertest/cucumber/stepDefinitions/firefoxSD","com/cucumbertest/cucumber/cucumberConfig"},
            plugin = { "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"},
            dryRun = false
    )
    
    public class FireFoxRunnerTest {
}

在胶水部分,我指向我的 firefoxSD(firefox 步骤定义 class 所在的位置(chrome 也相同))。我还在 class 添加了注释的路径:

@CucumberContextConfiguration
@SpringBootTest

引用 M.P。科桑杰

Cucumber uses the @CucumberContextConfiguration annotated class to determine which class to use to configure the spring application context.

在 gradle 构建中,我只添加了一个粘合路径:

'--glue', 'com/cucumbertest/cucumber/stepDefinitions'

但错过了添加另一行:

'--glue', 'com/cucumbertest/cucumber/cucumberConfig'

这样,gradle 无法找到 运行 我的应用程序(测试)所需的所有内容。当我在年级添加第二行时,测试成功了。 工作代码在 Github.

此致, 约万