如何让黄瓜让Spring注入一步定义类?

我有一个SpringBootApplication执行Cucumber(不作为测试的一部分,但作为主要的应用程序。

我碰巧在Kotlin做到这一点,但那不重要。

这是应用程序类。 demo02的主体是从一些黄瓜来源复制,一直工作正常。 我认为这没什么不妥。

 package cutest import ... @CucumberOptions(tags = arrayOf("not @ignore"), features = arrayOf("sandbox/cutest/src/main/resources"), // features = arrayOf("src/main/resources"), format = arrayOf( "pretty", "html:target/site/cucumber/cucumber", "json:target/failsafe-reports/cucumber.json" ), strict = true) @SpringBootApplication open class CucumberApplication { @Bean open fun demo02() = CommandLineRunner { val cl : ClassLoader = javaClass.getClassLoader() val rl = MultiLoader(cl) val roFactory = RuntimeOptionsFactory(this.javaClass) val ro = roFactory.create() val classFinder : ClassFinder = ResourceLoaderClassFinder(rl, cl) val runtime : cucumber.runtime.Runtime = cucumber.runtime.Runtime(rl, classFinder, cl, ro) runtime.run() if(runtime.exitStatus().toInt() != 0) { throw RuntimeException("exit status: " + runtime.exitStatus()); } } } fun main(args: Array<String>) { SpringApplication.run(CucumberApplication::class.java, *args) } 

然后我有一个功能文件:

 Feature: Server is reachable Here I try to connect to my web server app @Basic Scenario: Can connect to REST service of the web server Given Cucumber is up and running Then I can connect to the REST web server 

她是简单的工作步骤定义,匹配和传递的步骤Given Cucumber is up and running

 package cutest import cucumber.api.java.en.Given class SomeSteps { class SomeStepDefs { @Given(value = """^Cucumber is up and running$""") fun a() { assert(true) } } } 

而现在有问题的步骤定义。 它有一些@Autowired注释,它似乎是有问题的:

 package cutest import ... class RestSteps { @Autowired lateinit var ssh : PortForwardSshR @Value("\${cutest.url}") private val url = "http://example.com:8080/app/rest" @Given(value = """^I can connect to the REST web server$""") fun connectRest() { .... } } 

当我运行这个我得到以下输出:

 Feature: Cucumber is present Cucumber framework must be up and running @Basic Scenario: Cucumber works Given Cucumber is up and running Feature: Reseller Interface is reachable Here I try to ... @Basic Scenario: Can connect to REST service of the web server Given Cucumber is up and running Then I can connect to the REST web server kotlin.UninitializedPropertyAccessException: lateinit property ssh has not been initialized at cutest.RestSteps.connectRest(RestSteps.kt:47) at ✽.I can connect to the CAPs ... Failed scenarios: ... cutest/rest.feature:7 2 Scenarios (1 failed, 1 passed) 3 Steps (1 failed, 2 passed) 

在堆栈跟踪中可能没有更多的信息,但是这里是:

 kotlin.UninitializedPropertyAccessException: lateinit property ssh has not been initialized at cutest.RestSteps.connectRest(RestSteps.kt:47) at ✽.I can connect to the CAPs ResellerInterface(...cutest/rest.feature:9) 2017-11-07 13:46:08.974 INFO 11433 --- [ restartedMain] utoConfigurationReportLoggingInitializer : Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2017-11-07 13:46:08.985 ERROR 11433 --- [ restartedMain] osboot.SpringApplication : Application startup failed java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:304) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at cutest.CucumberApplicationKt.main(CucumberApplication.kt:50) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.7.RELEASE.jar:1.5.7.RELEASE] Caused by: java.lang.RuntimeException: exit status: 1 at vc.capper.cutest.CucumberApplication$demo02$1.run(CucumberApplication.kt:44) ~[classes/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] ... 11 common frames omitted