Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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
Unit testing 如何使用micronaut中的单元测试方法进行依赖性注入_Unit Testing_Kotlin_Dependency Injection_Inversion Of Control_Micronaut - Fatal编程技术网

Unit testing 如何使用micronaut中的单元测试方法进行依赖性注入

Unit testing 如何使用micronaut中的单元测试方法进行依赖性注入,unit-testing,kotlin,dependency-injection,inversion-of-control,micronaut,Unit Testing,Kotlin,Dependency Injection,Inversion Of Control,Micronaut,我有一个控制器类: @Controller("/x") class Controller { @Get fun index(): String { return "test test" } @Post fun dopost (userid) = executelogin(login.userId) } 此控制器取决于下面的executelogin方法: fun executelogin(userid) = do things 如果我想

我有一个控制器类:

@Controller("/x")
class Controller {

@Get
fun index(): String {
    return "test test"
}

@Post
fun dopost (userid) = executelogin(login.userId)
}
此控制器取决于下面的executelogin方法:

fun executelogin(userid) =
   do things
如果我想编写一个单元测试,我将如何使用依赖注入来引入executelogin方法的一个版本。以下是我到目前为止的情况,我不确定我是否做得正确:

@MicronautTest
class Controllertest(val method: executelogin()) {



@Inject
    lateinit var client: RxHttpClient


@Test
fun testController(){
    val request: HttpRequest<String> = HttpRequest.POST("/hello","test")
    val body: String = client.toBlocking().retrieve(request)

    assertNotNull(body)
    assertEquals(method,method)
    
}
}
@MicronautTest
类Controllertest(val方法:executelogin()){
@注入
lateinit变量客户端:RxHttpClient
@试验
趣味测试控制器(){
val请求:HttpRequest=HttpRequest.POST(“/hello”,“test”)
val body:String=client.toBlocking().retrieve(请求)
assertNotNull(正文)
资产质量(方法,方法)
}
}
在上面的单元测试中,我将executelogin方法作为controllertest类的参数,我认为这称为方法注入。我想知道这是否是正确的方法,或者是否需要进行调整。如果有些代码不太合理,我很抱歉,我对micronaut和依赖注入的概念还不熟悉

如果我想写一个单元测试,我将如何使用 依赖注入引入了一个版本的executelogin 方法

您不能使用依赖项注入来引入
executelogin
方法的版本,至少不能直接引入。Micronaut不支持方法注入

这不是你要的,但仅供参考。。。通常要做的一件事是注入一些包含要调用的方法的对象