在以下问题上,我需要您的帮助:无法 mock Fuel.get call 。
我尝试过的

这是服务类,将在其中调用Fuel。

class JiraService {
    private val logger: Logger = LoggerFactory.getLogger(JiraService::class.java)

    fun checkIfUsersExistInJira(usernames: List<String>, jiraConfig: Configuration): Boolean {
        var checkResultOk = true;
        val encodedCredentials =

        usernames.forEach {
            Fuel.get("${jiraConfig[url]}/rest/api/2/groupuserpicker?query=${it}&maxResults=1")
                .appendHeader("Authorization", "Basic ...")
                .appendHeader("Content-Type", "application/json")
                .responseObject(UserInfoDeserializer)
                .third
                .fold(
                    failure = { throwable ->
                        logger.error(
                            "Can't check users in jira for user $it",
                            throwable
                        )
                    },
                    success = { userExists ->
                        checkResultOk = checkResultOk && userExists
                    }
                )
        }
        return checkResultOk
    }
}

考试:
@ExtendWith(MockKExtension::class)
class JiraServiceTest {

    private val jiraConfig = createJiraConf()
    private val fuelRequest = mockk<Request>()
    private val fuelResponse = mockk<Response>()
    private val fuelMock = mockk<Fuel>()

    @BeforeEach
    fun setUp() {
        mockkStatic(FuelManager::class)
        every {
            fuelMock.get(eq("${jiraConfig[url]}/rest/api/2/[email protected]&maxResults=1"))
                .appendHeader(eq("Authorization"), any())
                .appendHeader(eq("Content-Type"), eq("application/json"))
                .responseObject(UserInfoDeserializer)
        } returns ResponseResultOf(first = fuelRequest, second = fuelResponse, third = Result.success(false))
    }

    @Test
    fun `will return FALSE for user not existing in jira`() {
        // given
        val usernames = listOf("[email protected]", "[email protected]", "[email protected]")

        // when
        JiraService().checkIfUsersExistInJira(usernames, jiraConfig)

        // then
    }
}

而且我总是看到错误:
Caused by: some.url.net
    com.github.kittinunf.fuel.core.FuelError$Companion.wrap(FuelError.kt:86)
    com.github.kittinunf.fuel.toolbox.HttpClient.executeRequest(HttpClient.kt:39)
Caused by: java.net.UnknownHostException: some.url.net
...

因此,它始终会进行真正的Fuel调用而忽略fueMock。
我究竟做错了什么?

感谢帮助!

最佳答案

如果有人需要,可以进行以下测试:

@ExtendWith(MockKExtension::class)

类JiraServiceTest {
private val jiraConfig = createJiraConf()
private val fuelRequest1 = mockk<DefaultRequest>()
private val fuelRequest3 = mockk<DefaultRequest>()
private val fuelRequest4 = mockk<DefaultRequest>()
private val fuelResponse = mockk<Response>()

@BeforeEach
fun setUp() {
    mockkObject(Fuel)
    every {
        Fuel.get(eq("${jiraConfig[url]}/rest/api/2/[email protected]&maxResults=1"))
    } returns fuelRequest1
    every {
        Fuel.get(eq("${jiraConfig[url]}/rest/api/2/[email protected]&maxResults=1"))
    } returns fuelRequest3
    every {
        Fuel.get(eq("${jiraConfig[url]}/rest/api/2/[email protected]&maxResults=1"))
    } returns fuelRequest4

    every { fuelRequest1.appendHeader(any<String>(), any()) } returns fuelRequest1
    every { fuelRequest3.appendHeader(any<String>(), any()) } returns fuelRequest3
    every { fuelRequest4.appendHeader(any<String>(), any()) } returns fuelRequest4

    every { fuelRequest1.responseObject(UserInfoDeserializer) } returns ResponseResultOf(
        first = fuelRequest1,
        second = fuelResponse,
        third = Result.success(false)
    )
    every { fuelRequest3.responseObject(UserInfoDeserializer) } returns ResponseResultOf(
        first = fuelRequest3,
        second = fuelResponse,
        third = Result.success(true)
    )
    every { fuelRequest4.responseObject(UserInfoDeserializer) } returns ResponseResultOf(
        first = fuelRequest4,
        second = fuelResponse,
        third = Result.success(true)
    )
}

@Test
fun `will return FALSE for user not existing in jira`() {
    // given
    val usernames = listOf("[email protected]", "[email protected]", "[email protected]")

    // when
    val result = JiraService().checkIfUsersExistInJira(usernames, jiraConfig)

    // then
    assertThat(result, equalTo(false))
}

@Test
fun `will return TRUE for users existing in jira`() {
    // given
    val usernames = listOf("[email protected]", "[email protected]")

    // when
    val result = JiraService().checkIfUsersExistInJira(usernames, jiraConfig)

    // then
    assertThat(result, equalTo(true))
}

}

10-06 13:06