From 617fd90acc8140547957576c46f02cc329e6984d Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Tue, 27 Feb 2024 09:30:11 +0600 Subject: [PATCH] refactor: unit test of GplayHttpClient --- app/build.gradle | 2 ++ .../e/apps/gplay/GplyHttpClientTest.kt | 25 ++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 941bda11c..72dada80f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -234,9 +234,11 @@ dependencies { // Coroutines def coroutines_version = "1.6.0" + def test_kotlin_version = "1.6.0" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version" + testImplementation "org.jetbrains.kotlin:kotlin-test:$test_kotlin_version" // Room def roomVersion = "2.4.1" diff --git a/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt b/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt index b0217740b..f83637ca6 100644 --- a/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt +++ b/app/src/test/java/foundation/e/apps/gplay/GplyHttpClientTest.kt @@ -44,6 +44,7 @@ import org.mockito.Mock import org.mockito.Mockito import org.mockito.MockitoAnnotations import org.mockito.kotlin.any +import kotlin.test.assertFailsWith @OptIn(ExperimentalCoroutinesApi::class) class GplyHttpClientTest { @@ -122,31 +123,28 @@ class GplyHttpClientTest { @Test fun testPostAuthFailedWhenStatus429() = runTest { initMocksForStatus429() - try { - gPlayHttpClient.postAuth(FakeCall.FAKE_URL, "".toByteArray()) - } catch (e: Exception) { - assert429(e) + val exception = assertFailsWith { + gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), mapOf()) } + assert429(exception) } @Test fun testGetWithMapParamsFailedWhenStatus429() = runTest { initMocksForStatus429() - try { + val exception = assertFailsWith { gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), mapOf()) - } catch (e: Exception) { - assert429(e) } + assert429(exception) } @Test fun testGetWithStringParamsFailedWhenStatus429() = runTest { initMocksForStatus429() - try { - gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), "") - } catch (e: Exception) { - assert429(e) + val exception = assertFailsWith { + gPlayHttpClient.get(FakeCall.FAKE_URL, mapOf(), mapOf()) } + assert429(exception) } private fun initMocksForStatus401() { @@ -157,10 +155,7 @@ class GplyHttpClientTest { } private suspend fun assert429(e: Exception) { - assertTrue( - "Status429", - e is GplayHttpRequestException && e.status == GPlayHttpClient.STATUS_CODE_TOO_MANY_REQUESTS - ) + assertTrue(e is GplayHttpRequestException && e.status == GPlayHttpClient.STATUS_CODE_TOO_MANY_REQUESTS) val event = EventBus.events.first() assertTrue(event is AppEvent.TooManyRequests) } -- GitLab