1

当我使用特定的 java 风格模拟restTemplate.postForObject方法时出现错误。

使用 java 版本 11.0.11.j9-adpt

o.mockk.MockKException: Class cast exception happened. Probably type information was erased. In this case use `hint` before call to specify exact return type of a method. Caused by: java.lang.ClassCastException: java.lang.Object incompatible with com.fif.payments.api.sfc.connector.adapter.rest.model.ConvertToPointsResponse

使用 java 版本 11.0.11-zulu:成功运行

extra["kotestVersion"] = "4.3.2"
extra["mockkVersion"] = "1.11.0"

测试
class PointsRestAdapterTest : BehaviorSpec({

    val restTemplate: RestTemplate = mockk()
    val pointsRepositoryConfig: PointsRepositoryConfig = PointsRepositoryConfig().apply {
        baseUrl = "http://baseurl"
        convertPointsPath = "/convert-to-points"
    }

    val pointsRepository: PointsRepository = PointsRestAdapter(restTemplate, pointsRepositoryConfig)

    given("A `purchaseAmount`, an `amountToConvert` and `transactionDate`") {
        val purchaseAmount = 100
        val amountToConvert = 50
        val transactionDate = LocalDateTime.now().toIsoString()

        `when`("call points service and response is present") {
            every {
                restTemplate.postForObject(
                        any<String>(),
                        any<HttpEntity<ConvertToPointsRequest>>(),
                        eq(ConvertToPointsResponse::class.java)
                )
            } returns ConvertToPointsResponse(100)

            then("no exceptions should be thrown") {
                val result = shouldNotThrowAny {
                    pointsRepository.convert(purchaseAmount, amountToConvert, transactionDate)
                }

                result shouldBe 100
            }
        }
    }

})
4

0 回答 0