Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not test suspend function with runBlockingTest #3330

Closed
champChayangkoon opened this issue Mar 8, 2020 · 2 comments
Closed

Can not test suspend function with runBlockingTest #3330

champChayangkoon opened this issue Mar 8, 2020 · 2 comments

Comments

@champChayangkoon
Copy link

I'm testing service with runBlockingTest but not work, But if use runBlocking instead runBlockingTest it's work

error when use runBlockingTest
java.lang.IllegalStateException: This job has not completed yet

my code

class ServiceTest {
    @get:Rule
    val mainCoroutineRule = MainCoroutineRule()

    @get:Rule
    val instantExecutorRule = InstantTaskExecutorRule()

    private var mockWebServer = MockWebServer()
    private lateinit var service: Service

    @Before
    fun setup() {
        mockWebServer.start()
        service = Retrofit.Builder()
                .baseUrl(mockWebServer.url("/"))
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(Service::class.java)
    }

    @After
    fun tearDown() {
        mockWebServer.shutdown()
    }

    @Test(expected = HttpException::class)
    fun testService() {
        val mockResponse = MockResponse().setResponseCode(HttpURLConnection.HTTP_BAD_GATEWAY)
        mockWebServer.enqueue(mockResponse)
        mainCoroutineRule.runBlockingTest {
            val response = service.testService()
        }
    }
@jzbrooks
Copy link

jzbrooks commented Mar 9, 2020

Probably related: Kotlin/kotlinx.coroutines#1204

@JakeWharton
Copy link
Member

Yep, that's it. Can't be fixed by us. We use threads (or, more specifically, OkHttp does) to perform work asynchronously from the caller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants