Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用TestPropertyProvider并使用Kotlin向Micronaut注入RxHttpClient_Kotlin_Micronaut - Fatal编程技术网

如何使用TestPropertyProvider并使用Kotlin向Micronaut注入RxHttpClient

如何使用TestPropertyProvider并使用Kotlin向Micronaut注入RxHttpClient,kotlin,micronaut,Kotlin,Micronaut,注意-我是一个Java+Spring的家伙,正在尝试Kotlin+Micronaut 我试图在嵌入式服务启动后使用TestPropertyProvider设置属性。 只要我的测试类中没有构造函数参数,它就可以正常工作 我可以添加RxHttpClient作为构造函数参数,它可以很好地被注入 但是,我想从Micronaut注入RxHttpClient,并实现TestPropertyProvider 我尝试将@Inject添加到RxHttpClient中,但得到错误此注释不适用于目标“局部变量”[因为

注意-我是一个Java+Spring的家伙,正在尝试Kotlin+Micronaut

我试图在嵌入式服务启动后使用
TestPropertyProvider
设置属性。
只要我的测试类中没有构造函数参数,它就可以正常工作

我可以添加
RxHttpClient
作为构造函数参数,它可以很好地被注入

但是,我想从Micronaut注入
RxHttpClient
,并实现
TestPropertyProvider

我尝试将
@Inject
添加到
RxHttpClient
中,但得到错误
此注释不适用于目标“局部变量”
[因为测试主体是传递给超类的lambda]

如果没有
@Inject
,我会得到一个错误
lateinit属性客户端尚未初始化

我的基类具有
TestPropertyProvider
实现

abstract class ZeebeSpecification(body: AbstractStringSpec.() -> Unit): StringSpec(body), TestPropertyProvider {
    override fun getProperties(): MutableMap<String, String> {
        return mutableMapOf("orchestrator.management.client.brokerContactPoint" to IntegrationTestHarness.instance.getBroker())
    }
}

RxHttpClient
已注入,但
TestPropertyProvider
未评估

@MicronautTest
class ZeebeBroker1Test(@Client("/" val client: RxHttpClient) : ZeebeSpecification({


    ...tests

}) {}

我从等式中删除了基类,并让我的测试直接实现
TestPropertyProvider
,但它仍然失败

@MicronautTest
class ZeebeBroker1Test(@Client("/") var client: HttpClient) : BehaviorSpec(), TestPropertyProvider {

    init {
        ...tests
    }
    private fun getBroker(): String {
        return IntegrationTestHarness.instance.getBroker()
    }
    override fun getProperties(): MutableMap<String, String> {
        return mutableMapOf("orchestrator.management.client.brokerContactPoint" to getBroker())
    }
}
@MicronautTest
类ZeeberBroker1Test(@Client(“/”)var Client:HttpClient):BehaviorSpec(),TestPropertyProvider{
初始化{
…测试
}
private fun getBroker():字符串{
返回IntegrationTestHarness.instance.getBroker()
}
重写fun getProperties():MutableMap{
将mutableMapOf(“orchestrator.management.client.brokerContactPoint”返回给getBroker()
}
}
看起来与此问题相同,但我已经在使用v1.1.2

如果我尝试使用
@Inject@Client(“/”)Client:RxHttpClient
它将抛出错误
消息:缺少类型为io.micronaut.http.Client.DefaultHttpClient的bean参数[LoadBalancer LoadBalancer]。必需参数:LoadBalancer


如何使用
TestPropertyProvider
和injected
RxHttpClient

我通过将规范主体移动到
init
中,并将
RxHttpClient
作为字段注入,解决了这个问题

@MicronautTest
class ZeebeBroker1Test() : ZeebeSpecification() {

    @Inject @field:Client("/") lateinit var client: RxHttpClient

    private val log: Logger = LoggerFactory.getLogger(ZeebeBroker1Test::class.java)

    init {
        "test case 1" {
            ...
        }

        "test case 2" {
            ...
        }
    }
}
我让基类实现
TestPropertyProvider
接口

abstract class ZeebeSpecification(): StringSpec(), TestPropertyProvider {

    open fun getBroker(): String {
        return IntegrationTestHarness.instance.getBroker()
    }

    override fun getProperties(): MutableMap<String, String> {
        return mutableMapOf("orchestrator.management.client.brokerContactPoint" to getBroker())
    }
}
抽象类ZeebeSpecification():StringSpec(),TestPropertyProvider{
打开getBroker():字符串{
返回IntegrationTestHarness.instance.getBroker()
}
重写fun getProperties():MutableMap{
将mutableMapOf(“orchestrator.management.client.brokerContactPoint”返回给getBroker()
}
}
abstract class ZeebeSpecification(): StringSpec(), TestPropertyProvider {

    open fun getBroker(): String {
        return IntegrationTestHarness.instance.getBroker()
    }

    override fun getProperties(): MutableMap<String, String> {
        return mutableMapOf("orchestrator.management.client.brokerContactPoint" to getBroker())
    }
}