rybalkinsd / kohttp

Kotlin DSL http client
https://kohttp.gitbook.io
Apache License 2.0
478 stars 42 forks source link

[kohttp-mockk] Mockk integration #161

Open neisip opened 4 years ago

neisip commented 4 years ago

It would be really nice to have ability to somehow swap defaultHttpClient: okhttp3.Call.Factory with Mock Entity in tests. For now current implementation of functions like httpGet {} can be only tested by using MockWebServer from okhttp or you have to inject okhttp3.Call.Factory manually.

Ideally i would like to have integration with mockk, because it's designated mocking framework for kotlin.

Example:

@EnableOkHttpMock
class MyTest {

    private val sut = MyBestService()

    fun `test foo is bar`() {
        //given
        every { httpGet(any()) } returns "foo" to "bar"

        //when
        val res = sut.getBar()

        //then
        assert("bar", res)
    }
}
rybalkinsd commented 4 years ago

There is a available out of the box now:

 mockkStatic("io.github.rybalkinsd.kohttp.dsl.HttpGetDslKt")
 every { httpGet(any(), any()) } returns Response.Builder()....build()

Need to add separate module, which will invoke all the static mocks initialisation. Also need some sugar