简体   繁体   English

“lateinit 属性<varname>在 Kotlin 上使用带有 SpringBootTest 的 WebTestClient 时尚未初始化”</varname>

[英]“lateinit property <varName> has not been initialized” when using WebTestClient with SpringBootTest on Kotlin

I can not figure out how to get the WebTestClient initialized when using Kotlin and Spring Boot Tests.在使用 Kotlin 和 Spring 引导测试时,我无法弄清楚如何初始化WebTestClient

@ExtendWith(SpringExtension::class, MockKExtension::class)
@AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class GraphQlClientIntegrationTest {
    private val testToken = "aUna%14OkmUZunb528342"

    @Autowired
    lateinit var client: WebTestClient

    @InjectMockKs
    lateinit var gateGraphQlClient: GateGraphQlClient

    @Test
    fun shouldAddAuthorization() {
        val ID = Id(UUID.randomUUID().toString())
        val returnResult = client.post()
                .uri("/graphql")
                .header(HttpHeaders.AUTHORIZATION, testToken)
                .exchange()
                .expectStatus().isOk
                .returnResult(ObjectNode::class.java)
    }
}

This is the error message这是错误信息

lateinit property client has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property client has not been initialized

Try constructor injection.尝试构造函数注入。 Because lateinit will initialize object lazily.因为lateinit会懒惰地初始化object。 So it will give null value when you call method so its throw UninitializedPropertyAccessException因此,当您调用方法时它会给出null值,因此它会抛出UninitializedPropertyAccessException

class GraphQlClientIntegrationTest(val client: WebTestClient,
                                   val gateGraphQlClient: GateGraphQlClient) {
    private val testToken = "aUna%14OkmUZunb528342"
    // Your Code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 Spring DI 的 Kotlin:lateinit 属性尚未初始化 - Kotlin with Spring DI: lateinit property has not been initialized Kotlin + SpringBoot:DI lateinit属性服务尚未初始化 - Kotlin + SpringBoot : DI lateinit property service has not been initialized kotlin.UninitializedPropertyAccessException:lateinit 属性尚未初始化 - kotlin.UninitializedPropertyAccessException: lateinit property has not been initialized 使用@Autowired 和@Component 时出现lateinit 属性未初始化错误 - Got lateinit property has not been initialized error when using @Autowired and @Component spring::lateinit 属性 userRepo 尚未初始化 - spring:: lateinit property userRepo has not been initialized @Autowired 但得到了 lateinit 属性 testBean has not been initialized 错误 - @Autowired but got lateinit property testBean has not been initialized error 从`.properties`文件中检索值| lateinit属性尚未初始化 - Retrieve values from `.properties` file | lateinit property has not been initialized 使用 WebTestClient 在 springBootTest 中禁用安全性 - Disable security in springBootTest using WebTestClient 存储库插入在带有 WebTestClient 的 SpringBootTest 中不起作用 - Repository inserts don't work in SpringBootTest with WebTestClient 使用spring-webflux时WebTestClient不起作用 - WebTestClient does not work when using spring-webflux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM