Skip to content

Commit a6a801a

Browse files
Thomas Nardonefacebook-github-bot
authored andcommitted
Update ReactOkHttpNetworkFetcherTest (facebook#48205)
Summary: Flip mockito usages to mockito-kotlin Changelog: [Internal] Reviewed By: cortinico Differential Revision: D66985841
1 parent 3262cfa commit a6a801a

File tree

1 file changed

+51
-58
lines changed

1 file changed

+51
-58
lines changed

packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/fresco/ReactOkHttpNetworkFetcherTest.kt

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,16 @@ import org.assertj.core.api.Assertions.assertThat
2222
import org.junit.Before
2323
import org.junit.Test
2424
import org.junit.runner.RunWith
25-
import org.mockito.ArgumentCaptor
26-
import org.mockito.Captor
27-
import org.mockito.Mockito.any
28-
import org.mockito.Mockito.mock
29-
import org.mockito.Mockito.spy
30-
import org.mockito.Mockito.times
31-
import org.mockito.Mockito.verify
32-
import org.mockito.Mockito.`when` as whenever
25+
import org.mockito.kotlin.KArgumentCaptor
26+
import org.mockito.kotlin.any
27+
import org.mockito.kotlin.argumentCaptor
28+
import org.mockito.kotlin.mock
29+
import org.mockito.kotlin.spy
30+
import org.mockito.kotlin.times
31+
import org.mockito.kotlin.verify
32+
import org.mockito.kotlin.whenever
3333
import org.robolectric.RobolectricTestRunner
3434

35-
/**
36-
* Returns Mockito.any() as nullable type to avoid java.lang.IllegalStateException when null is
37-
* returned.
38-
*/
39-
private fun <T> anyOrNull(type: Class<T>): T = any(type)
40-
41-
/**
42-
* Returns ArgumentCaptor.capture() as nullable type to avoid java.lang.IllegalStateException when
43-
* null is returned.
44-
*/
45-
fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture()
46-
4735
@RunWith(RobolectricTestRunner::class)
4836
class ReactOkHttpNetworkFetcherTest {
4937
private lateinit var httpClient: OkHttpClient
@@ -52,28 +40,28 @@ class ReactOkHttpNetworkFetcherTest {
5240
private lateinit var callback: NetworkFetcher.Callback
5341
private lateinit var imageRequest: ReactNetworkImageRequest
5442

55-
@Captor private lateinit var requestArgumentCaptor: ArgumentCaptor<Request>
43+
private lateinit var requestArgumentCaptor: KArgumentCaptor<Request>
5644

5745
@Before
5846
fun prepareModules() {
59-
val executorService = mock(ExecutorService::class.java)
47+
val executorService = mock<ExecutorService>()
6048
val dispatcher = Dispatcher(executorService)
6149
httpClient = spy(OkHttpClient.Builder().dispatcher(dispatcher).build())
6250
fetcher = ReactOkHttpNetworkFetcher(httpClient)
6351

64-
fetchState = mock(OkHttpNetworkFetcher.OkHttpNetworkFetchState::class.java)
65-
callback = mock(NetworkFetcher.Callback::class.java)
52+
fetchState = mock()
53+
callback = mock()
6654

6755
val mockUri = Uri.parse("https://www.facebook.com")
6856
whenever(fetchState.uri).thenReturn(mockUri)
6957

70-
val producerContext = mock(ProducerContext::class.java)
71-
imageRequest = mock(ReactNetworkImageRequest::class.java)
58+
val producerContext = mock<ProducerContext>()
59+
imageRequest = mock()
7260
whenever(imageRequest.headers).thenReturn(null)
7361
whenever(producerContext.imageRequest).thenReturn(imageRequest)
7462
whenever(fetchState.context).thenReturn(producerContext)
7563

76-
requestArgumentCaptor = ArgumentCaptor.forClass(Request::class.java)
64+
requestArgumentCaptor = argumentCaptor()
7765
}
7866

7967
@Test
@@ -82,14 +70,14 @@ class ReactOkHttpNetworkFetcherTest {
8270

8371
fetcher.fetch(fetchState, callback)
8472

85-
verify(httpClient, times(1)).newCall(anyOrNull(Request::class.java))
86-
verify(httpClient).newCall(capture(requestArgumentCaptor))
87-
88-
val capturedRequest = requestArgumentCaptor.value
89-
90-
assertThat(capturedRequest.cacheControl().noStore()).isEqualTo(true)
91-
assertThat(capturedRequest.headers()["Cache-Control"]).isEqualTo("no-store")
92-
assertThat(capturedRequest.headers().size()).isEqualTo(1)
73+
with(requestArgumentCaptor) {
74+
verify(httpClient, times(1)).newCall(any())
75+
verify(httpClient).newCall(capture())
76+
val capturedRequest = firstValue
77+
assertThat(capturedRequest.cacheControl().noStore()).isEqualTo(true)
78+
assertThat(capturedRequest.headers()["Cache-Control"]).isEqualTo("no-store")
79+
assertThat(capturedRequest.headers().size()).isEqualTo(1)
80+
}
9381
}
9482

9583
@Test
@@ -98,15 +86,17 @@ class ReactOkHttpNetworkFetcherTest {
9886

9987
fetcher.fetch(fetchState, callback)
10088

101-
verify(httpClient, times(1)).newCall(anyOrNull(Request::class.java))
102-
verify(httpClient).newCall(capture(requestArgumentCaptor))
89+
with(requestArgumentCaptor) {
90+
verify(httpClient, times(1)).newCall(any())
91+
verify(httpClient).newCall(capture())
10392

104-
val capturedRequest = requestArgumentCaptor.value
93+
val capturedRequest = firstValue
10594

106-
assertThat(capturedRequest.cacheControl().noCache()).isEqualTo(true)
107-
assertThat(capturedRequest.cacheControl().noStore()).isEqualTo(true)
108-
assertThat(capturedRequest.headers()["Cache-Control"]).isEqualTo("no-cache, no-store")
109-
assertThat(capturedRequest.headers().size()).isEqualTo(1)
95+
assertThat(capturedRequest.cacheControl().noCache()).isEqualTo(true)
96+
assertThat(capturedRequest.cacheControl().noStore()).isEqualTo(true)
97+
assertThat(capturedRequest.headers()["Cache-Control"]).isEqualTo("no-cache, no-store")
98+
assertThat(capturedRequest.headers().size()).isEqualTo(1)
99+
}
110100
}
111101

112102
@Test
@@ -115,15 +105,16 @@ class ReactOkHttpNetworkFetcherTest {
115105

116106
fetcher.fetch(fetchState, callback)
117107

118-
verify(httpClient, times(1)).newCall(anyOrNull(Request::class.java))
119-
verify(httpClient).newCall(capture(requestArgumentCaptor))
120-
121-
val capturedRequest = requestArgumentCaptor.value
108+
with(requestArgumentCaptor) {
109+
verify(httpClient, times(1)).newCall(any())
110+
verify(httpClient).newCall(capture())
111+
val capturedRequest = firstValue
122112

123-
assertThat(capturedRequest.cacheControl().maxStaleSeconds()).isEqualTo(Integer.MAX_VALUE)
124-
assertThat(capturedRequest.headers()["Cache-Control"])
125-
.isEqualTo("max-stale=${Integer.MAX_VALUE}")
126-
assertThat(capturedRequest.headers().size()).isEqualTo(1)
113+
assertThat(capturedRequest.cacheControl().maxStaleSeconds()).isEqualTo(Integer.MAX_VALUE)
114+
assertThat(capturedRequest.headers()["Cache-Control"])
115+
.isEqualTo("max-stale=${Integer.MAX_VALUE}")
116+
assertThat(capturedRequest.headers().size()).isEqualTo(1)
117+
}
127118
}
128119

129120
@Test
@@ -132,15 +123,17 @@ class ReactOkHttpNetworkFetcherTest {
132123

133124
fetcher.fetch(fetchState, callback)
134125

135-
verify(httpClient, times(1)).newCall(anyOrNull(Request::class.java))
136-
verify(httpClient).newCall(capture(requestArgumentCaptor))
126+
with(requestArgumentCaptor) {
127+
verify(httpClient, times(1)).newCall(any())
128+
verify(httpClient).newCall(capture())
137129

138-
val capturedRequest = requestArgumentCaptor.value
130+
val capturedRequest = firstValue
139131

140-
assertThat(capturedRequest.cacheControl().onlyIfCached()).isEqualTo(true)
141-
assertThat(capturedRequest.cacheControl().maxStaleSeconds()).isEqualTo(Integer.MAX_VALUE)
142-
assertThat(capturedRequest.headers()["Cache-Control"])
143-
.isEqualTo("max-stale=${Integer.MAX_VALUE}, only-if-cached")
144-
assertThat(capturedRequest.headers().size()).isEqualTo(1)
132+
assertThat(capturedRequest.cacheControl().onlyIfCached()).isEqualTo(true)
133+
assertThat(capturedRequest.cacheControl().maxStaleSeconds()).isEqualTo(Integer.MAX_VALUE)
134+
assertThat(capturedRequest.headers()["Cache-Control"])
135+
.isEqualTo("max-stale=${Integer.MAX_VALUE}, only-if-cached")
136+
assertThat(capturedRequest.headers().size()).isEqualTo(1)
137+
}
145138
}
146139
}

0 commit comments

Comments
 (0)