我一整天都在嘗試解決這個問題,但沒能做到。所以讓我們來描述一下: 我的意圖:在 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("***************************************************************");
}
}
然后我創建了跑步者類,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 {
}
再創建三個類: ConfigLoad:
@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 {
}
構建.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 時,第一個然后另一個,測驗運行得很好,但是當我通過 gradle 運行測驗時: 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$0(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 類)注釋類,但收到此錯誤。
這是 git 上的代碼:Github 代碼
我做錯了什么或我在實施中錯過了什么?
最好的問候,約萬。
uj5u.com熱心網友回復:
Cucumber 使用帶@CucumberContextConfiguration注釋的類來確定使用哪個類來配置 Spring 應用程式背景關系。
這個類必須在粘合路徑上。
在FireFoxRunnerTest您有兩條這樣的路徑:
glue ={
"com/cucumbertest/cucumber/stepDefinitions/firefoxSD",
"com/cucumbertest/cucumber/cucumberConfig"
},
但是,在任務中,cucumberCLI您使用單個膠水引數運行 Cucumber:
'--glue', 'com/digitalautomationsolution/dascucumber/stepDefinitions',
仔細查看差異。
uj5u.com熱心網友回復:
其他人的解決方案:在我的 Firefox 跑步者(和 chrome 跑步者)中,我有以下幾行:
@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 步驟定義類(對于 chrome 也相同))。我還添加了我有注釋的類的路徑:
@CucumberContextConfiguration
@SpringBootTest
參考 MP Korsanje
Cucumber 使用@CucumberContextConfiguration 注解的類來確定使用哪個類來配置spring 應用程式背景關系。
在 gradle build 中,我只添加了一條膠水路徑:
'--glue', 'com/cucumbertest/cucumber/stepDefinitions'
但錯過了添加另一行:
'--glue', 'com/cucumbertest/cucumber/cucumberConfig'
這樣,gradle 無法找到運行我的應用程式(測驗)所需的所有東西。當我在年級中添加第二行時,測驗成功了。作業代碼在 Github 上。
最好的問候, 約萬
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/467625.html
上一篇:使用libnet.lingala.zip4j時,JavafxGradle使例外org.gradle.process.internal.ExecException以非零退出值1結束
