[英]“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.