gpt4 book ai didi

kotlin - 无法使用Mockk模拟com.github.kittinunf.fuel.Fuel

转载 作者:行者123 更新时间:2023-12-02 13:43:57 26 4
gpt4 key购买 nike

在以下问题上,我需要您的帮助:无法 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/groupuserpicker?query=user_1@test.com&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("user_1@test.com", "user_3@test.com", "user_4@test.com")

// 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/groupuserpicker?query=user_1@test.com&maxResults=1"))
} returns fuelRequest1
every {
Fuel.get(eq("${jiraConfig[url]}/rest/api/2/groupuserpicker?query=user_3@test.com&maxResults=1"))
} returns fuelRequest3
every {
Fuel.get(eq("${jiraConfig[url]}/rest/api/2/groupuserpicker?query=user_4@test.com&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("user_1@test.com", "user_3@test.com", "user_4@test.com")

// 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("user_3@test.com", "user_4@test.com")

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

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

}

关于kotlin - 无法使用Mockk模拟com.github.kittinunf.fuel.Fuel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60833000/

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