has not been initialized"-6ren"> has not been initialized"-我不知道如何获得 WebTestClient使用 Kotlin 和 Spring Boot 测试时初始化。 @ExtendWith(SpringExtension::class, MockKExten-6ren">
gpt4 book ai didi

spring - 在 Kotlin 上使用 WebTestClient 和 SpringBootTest 时出现 "lateinit property has not been initialized"

转载 作者:行者123 更新时间:2023-12-02 13:35:31 27 4
gpt4 key购买 nike

我不知道如何获得 WebTestClient使用 Kotlin 和 Spring Boot 测试时初始化。

@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)
}
}

这是错误信息
lateinit property client has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property client has not been initialized

最佳答案

尝试构造函数注入(inject)。因为 lateinit 会延迟初始化对象。所以它会给null调用方法时的值,因此它会抛出 UninitializedPropertyAccessException

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

关于spring - 在 Kotlin 上使用 WebTestClient 和 SpringBootTest 时出现 "lateinit property <varName> has not been initialized",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58271075/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com