升级 spring 3.1 时遇到有关 HttpStatusCode 的问题

问题描述 投票:0回答:1

伙计们 当我将 spring 2.65 升级到 3.1.0 时,我遇到了一个奇怪的问题。 这是代码:

import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatus.OK
import org.springframework.http.HttpStatusCode
import org.springframework.web.server.ServerWebExchange


    @Test
    fun `and the operation is successful it should create and emit the audit event`() {
        private val mockExchange = mockk<ServerWebExchange>()
        every { mockExchange.authorizationData() } returns null
        every { mockExchange.response.statusCode } returns HttpStatus.OK

        mockSuccessfulFilterOperations()

        assertDoesNotThrow {
            auditLoggerFilter.filter(mockExchange, mockChain).block()
        }

        verify(exactly = 1) { auditLoggerService.publishAuditEvent(any(), SUCCESS) }
        confirmVerified(auditLoggerService)
    }

异常(每个 {mockExchange.response.statusCode } 返回 HttpStatus.OK):

org.springframework.http.HttpStatusCode
java.lang.InstantiationError: org.springframework.http.HttpStatusCode

但是我读了doc,HttpStatus并没有被弃用,为什么它有异常?

我尝试使用:

every { mockExchange.response.statusCode } returns HttpStatusCode.valueOf(200)

还是不行。

kotlin spring-mvc junit
1个回答
0
投票

问题是某些方法使用

HttpStatusCode
参数。我假设这样的方法在
auditLoggerService
中并且它是
publishAuditEvent()
。如果这个参数类型是
HttpStatusCode
,那么需要改为
HttpStatus
。因此,会出现错误,但这并没有什么问题,只需通过
HttpStatus
运算符将此参数转换为
as
类型即可。

测试哪里有这样的异常:

@Test
fun `log http response`(): Unit = runBlocking {
    val loggingService = mockk<IntegrationLoggingService> {
        every { logHttpResponse(any(), any()) } returns mockk()
    }
    val responseDecorator = HttpResponseLoggingDecorator(
        loggingService,
        MockServerHttpResponse().apply { statusCode = HttpStatus.BAD_GATEWAY }
    )

    responseDecorator.writeWith(messageBodyFlux()).awaitSingleOrNull()

    coVerify(exactly = 2) { loggingService.logHttpResponse(any(), any()) }
}

HttpStatusCode
参数转换为
HttpStatus
类型的方法:

loggingService.logHttpResponse(body = null, statusCode as HttpStatus)
© www.soinside.com 2019 - 2024. All rights reserved.