Java 如何让Cucumber让Spring注入步骤定义类?

Java 如何让Cucumber让Spring注入步骤定义类?,java,spring,spring-boot,cucumber,kotlin,Java,Spring,Spring Boot,Cucumber,Kotlin,我有一个springboot应用程序,它执行Cucumber(不是作为测试的一部分,而是作为主应用程序) 我碰巧在科特林这样做,但那不重要 这是应用程序类。demo02的主体是从一些Cucumber源代码复制而来的,一直运行良好。我认为这没什么问题 package cutest import ... @CucumberOptions(tags = arrayOf("not @ignore"), features = arrayOf("sandbox/cutest/sr

我有一个springboot应用程序,它执行Cucumber(不是作为测试的一部分,而是作为主应用程序)

我碰巧在科特林这样做,但那不重要

这是应用程序类。
demo02
的主体是从一些Cucumber源代码复制而来的,一直运行良好。我认为这没什么问题

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)
}
以下是简单的工作步骤定义,匹配并传递给定Cumber启动并运行的步骤

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)
stacktrace中可能没有更多的信息,但这里是:

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] o.s.boot.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

您是否将
cucumberspring
包含为依赖项?我写了一篇关于cucumber和使用spring的依赖项注入的博客文章。也许它可以给你一个提示,你有Bean
PortForwardsHR
?@Marit我想我有,但我可以再次检查你是否将
cucumber spring
作为依赖项?我写了一篇关于cucumber和使用spring的依赖项注入的博客文章。也许它可以给你一个提示,你有Bean
PortForwardSshR
?@Marit我想我有,但我可以再检查一下
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] o.s.boot.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