From e0f907961a365da5637994864a892e6163ca4f3e Mon Sep 17 00:00:00 2001 From: Thomas Nardone Date: Mon, 9 Dec 2024 14:05:49 -0800 Subject: [PATCH] Fix up network module tests (#48160) Summary: Shift to mockito-kotlin and fix ReactCookieJarContainerTest so it's not just testing a mock. Changelog: [Internal] Differential Revision: D66898956 --- .../network/ReactCookieJarContainerTest.kt | 33 ++++++++----------- .../react/modules/network/ResponseUtilTest.kt | 9 +++-- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/ReactCookieJarContainerTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/ReactCookieJarContainerTest.kt index a84304166cc5b0..e9dac8494e8d35 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/ReactCookieJarContainerTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/ReactCookieJarContainerTest.kt @@ -13,17 +13,11 @@ import okhttp3.HttpUrl import org.assertj.core.api.Assertions.assertThat import org.junit.Test import org.junit.runner.RunWith -import org.mockito.Mockito.any -import org.mockito.Mockito.mock -import org.mockito.Mockito.`when` as whenever +import org.mockito.kotlin.any +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever import org.robolectric.RobolectricTestRunner -/** - * Returns Mockito.any() as nullable type to avoid java.lang.IllegalStateException when null is - * returned. - */ -private fun nonNullAny(type: Class): T = any(type) - /** Tests for {@link NetworkingModule}. */ @RunWith(RobolectricTestRunner::class) class ReactCookieJarContainerTest { @@ -31,25 +25,26 @@ class ReactCookieJarContainerTest { @Test fun testMissingJar() { - val jarContainer: ReactCookieJarContainer = mock(ReactCookieJarContainer::class.java) + val jarContainer = ReactCookieJarContainer() assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(0) } @Test fun testEmptyCookies() { - val jarContainer: ReactCookieJarContainer = mock(ReactCookieJarContainer::class.java) - val cookies: List = emptyList() - whenever(jarContainer.loadForRequest(nonNullAny(HttpUrl::class.java))).thenReturn(cookies) + val jarContainer = ReactCookieJarContainer() + val cookieJar = mock() + jarContainer.setCookieJar(cookieJar) + whenever(cookieJar.loadForRequest(any())).thenReturn(emptyList()) assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(0) } @Test fun testValidCookies() { val jarContainer = ReactCookieJarContainer() - val cookieJar: CookieJar = mock(CookieJar::class.java) + val cookieJar = mock() jarContainer.setCookieJar(cookieJar) - val cookies: MutableList = mutableListOf() - cookies.add(Cookie.Builder().name("valid").value("valid value").domain("domain").build()) + val cookies = + listOf(Cookie.Builder().name("valid").value("valid value").domain("domain").build()) whenever(cookieJar.loadForRequest(httpUrl)).thenReturn(cookies) assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(1) } @@ -57,10 +52,10 @@ class ReactCookieJarContainerTest { @Test fun testInvalidCookies() { val jarContainer = ReactCookieJarContainer() - val cookieJar: CookieJar = mock(CookieJar::class.java) + val cookieJar = mock() jarContainer.setCookieJar(cookieJar) - val cookies: MutableList = mutableListOf() - cookies.add(Cookie.Builder().name("valid").value("înválíd välūė").domain("domain").build()) + val cookies = + listOf(Cookie.Builder().name("valid").value("înválíd välūė").domain("domain").build()) whenever(cookieJar.loadForRequest(httpUrl)).thenReturn(cookies) assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(0) } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/ResponseUtilTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/ResponseUtilTest.kt index 4330b760ec3f9f..7c93c321292c93 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/ResponseUtilTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/network/ResponseUtilTest.kt @@ -20,8 +20,11 @@ import org.junit.Before import org.junit.Test import org.mockito.ArgumentCaptor import org.mockito.MockedStatic -import org.mockito.Mockito.* import org.mockito.Mockito.mockStatic +import org.mockito.kotlin.any +import org.mockito.kotlin.mock +import org.mockito.kotlin.never +import org.mockito.kotlin.verify class ResponseUtilTest { private lateinit var reactContext: ReactApplicationContext @@ -29,7 +32,7 @@ class ResponseUtilTest { @Before fun setUp() { - reactContext = mock(ReactApplicationContext::class.java) + reactContext = mock() arguments = mockStatic(Arguments::class.java) arguments.`when`(Arguments::createArray).thenAnswer { JavaOnlyArray() } @@ -247,6 +250,6 @@ class ResponseUtilTest { ResponseUtil.onRequestSuccess(null, 1) ResponseUtil.onResponseReceived(null, 1, 200, Arguments.createMap(), "http://example.com") - verify(reactContext, never()).emitDeviceEvent(anyString(), any()) + verify(reactContext, never()).emitDeviceEvent(any(), any()) } }