Skip to content

Commit

Permalink
tests: update GET request test to wait for task to finish
Browse files Browse the repository at this point in the history
Previously, the `ParselyAPIConnection` task was not finishing execution before the test finished. Only because the task's `onBackground` was fast, the test was passing. To remove this flakiness, the introduced change uses LooperMode.Mode.PAUSED to wait for AsyncTask execution to finish, before running assertions.
  • Loading branch information
wzieba committed Oct 20, 2023
1 parent bae3ca3 commit ab4fe3a
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.parsely.parselyandroid

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.LooperMode
import org.robolectric.shadows.ShadowLooper.shadowMainLooper

@RunWith(RobolectricTestRunner::class)
@LooperMode(LooperMode.Mode.PAUSED)
class ParselyAPIConnectionTest {

private lateinit var sut: ParselyAPIConnection
Expand All @@ -23,11 +26,20 @@ class ParselyAPIConnectionTest {

@Test
fun `when making connection without any events, then make GET request`() {
// given
mockServer.enqueue(MockResponse().setResponseCode(200))

// when
sut.execute(mockServer.url("/").toString())
val url = mockServer.url("").toString()
sut.execute(url).get()
shadowMainLooper().idle();

// then
assertThat(mockServer.takeRequest().method).isEqualTo("GET")
val request = mockServer.takeRequest()
assertThat(request).satisfies({
assertThat(it.method).isEqualTo("GET")
assertThat(it.failure).isNull()
})
}

@Test
Expand Down

0 comments on commit ab4fe3a

Please sign in to comment.