1
1
package com.baeldung.retrofit
2
2
3
3
import kotlinx.coroutines.runBlocking
4
- import okhttp3.mockwebserver.MockResponse
5
- import okhttp3.mockwebserver.MockWebServer
6
- import org.junit.After
7
- import org.junit.Before
8
4
import org.junit.Test
9
5
import java.io.File
10
6
import kotlin.test.assertTrue
11
7
12
8
class FileDownloadServiceTest {
13
9
14
- private lateinit var mockWebServer: MockWebServer
15
-
16
- @Before
17
- fun setup () {
18
- mockWebServer = MockWebServer ()
19
- mockWebServer.start()
20
- }
21
-
22
- @After
23
- fun tearDown () {
24
- mockWebServer.shutdown()
25
- }
26
-
27
10
@Test
28
11
fun `test successful file download` (): Unit = runBlocking {
29
- // Mock response with sample PDF content
30
- val mockResponse = MockResponse ()
31
- .setResponseCode(200 )
32
- .setBody(" This is a mock PDF content" ) // Simulate PDF binary content
33
- mockWebServer.enqueue(mockResponse)
34
-
35
- // Get the mock server URL
36
- val mockUrl = mockWebServer.url(" /sample.pdf" ).toString()
37
12
38
13
// File to store the downloaded content
39
14
val outputFile = File (" test_sample.pdf" )
@@ -42,15 +17,12 @@ class FileDownloadServiceTest {
42
17
}
43
18
44
19
// Call the download function
45
- downloadPdfWithRetrofit(mockUrl, outputFile)
46
-
47
- // Verify the file was created and content matches
20
+ downloadPdfWithRetrofit(" https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" , outputFile)
48
21
22
+ // Verify the file was created
49
23
assertTrue(outputFile.exists())
50
- assertTrue(outputFile.readText() == " This is a mock PDF content" )
51
24
52
25
// Clean up the test file
53
26
outputFile.delete()
54
27
}
55
-
56
28
}
0 commit comments