Last active
September 16, 2023 01:29
-
-
Save Pouyaa91/c1901cc9f9cd3e40a889e1a1a1f5935a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CategoriesApiServiceTest { | |
private lateinit var apiService: CategoriesApiService | |
private lateinit var mockWebServer: MockWebServer | |
private val json = Json { ignoreUnknownKeys = true } | |
@Before | |
fun setup() { | |
mockWebServer = MockWebServer().apply(MockWebServer::start) | |
apiService = Retrofit.Builder() | |
.addConverterFactory(json.asConverterFactory("application/json".toMediaType())) | |
.baseUrl(mockWebServer.url("/")) | |
.build() | |
.create(CategoriesApiService::class.java) | |
} | |
@Test | |
fun checkCategoriesConvertsCorrectly() = runTest { | |
val mockResponse = File("src/test/resources/categories.json").readText() | |
mockWebServer.enqueue(MockResponse().setBody(mockResponse)) | |
val categories = apiService.getCategories().categories | |
assertEquals(categories.size, 2) | |
assertEquals(categories.getOrNull(0)?.id, "1") | |
assertEquals(categories.getOrNull(0)?.name, "first category") | |
assertEquals(categories.getOrNull(0)?.imageUrl, "first thumb") | |
assertEquals(categories.getOrNull(0)?.description, "first description") | |
} | |
@After | |
fun tearDown() { | |
mockWebServer.shutdown() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment