From ab4fe3a4be4d871f748f2a2c219de80e83cf8517 Mon Sep 17 00:00:00 2001 From: Wojtek Zieba Date: Fri, 20 Oct 2023 12:07:23 +0200 Subject: [PATCH] tests: update GET request test to wait for task to finish 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. --- .../parselyandroid/ParselyAPIConnectionTest.kt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/parsely/src/test/java/com/parsely/parselyandroid/ParselyAPIConnectionTest.kt b/parsely/src/test/java/com/parsely/parselyandroid/ParselyAPIConnectionTest.kt index 91982ad5..ed45f76b 100644 --- a/parsely/src/test/java/com/parsely/parselyandroid/ParselyAPIConnectionTest.kt +++ b/parsely/src/test/java/com/parsely/parselyandroid/ParselyAPIConnectionTest.kt @@ -1,7 +1,7 @@ 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 @@ -9,8 +9,11 @@ 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 @@ -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