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
@RunWith(AndroidJUnit4::class) // To indicate that we've to run it with AndroidJUnit runner | |
class CounterDisplayKtTest { | |
@get: Rule | |
val composeTestRule = createComposeRule() // compose rule is required to get access to the composable component | |
@Before | |
fun setUp() { | |
composeTestRule.setContent { // setting our composable as content for test | |
ApiTestingTheme { |
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
@Composable | |
fun CounterDisplay( | |
modifier: Modifier | |
) { | |
var editedText by remember { | |
mutableStateOf("") | |
} | |
var counterText by remember { |
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
@file:OptIn(ExperimentalCoroutinesApi::class) | |
class UserRepositoryImplTest { | |
private lateinit var repository: UserRepository | |
private lateinit var testApis: TestApis | |
private lateinit var mockWebServer: MockWebServer | |
@Before | |
fun setUp() { |
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
@Test | |
fun `for user id not available, api must return with http code 404 and null user object`() = runTest { | |
val expectedResponse = MockResponse() | |
.setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) | |
mockWebServer.enqueue(expectedResponse) | |
val actualResponse = repository.getUserById(1) | |
assertThat(actualResponse.httpCode).isEqualTo(HttpURLConnection.HTTP_NOT_FOUND) | |
assertThat(actualResponse.body).isNull() | |
} |
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
@Test | |
fun `for multiple users, api must return all users with http code 200`() = runTest { | |
val users = listOf( | |
User(1,"Saurabh","Delhi","India"), | |
User(2,"Zergain","London","UK"), | |
) | |
val expectedResponse = MockResponse() | |
.setResponseCode(HttpURLConnection.HTTP_OK) | |
.setBody(Gson().toJson(users)) | |
mockWebServer.enqueue(expectedResponse) |
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
private lateinit var repository: UserRepository | |
private lateinit var testApis: TestApis | |
private lateinit var mockWebServer: MockWebServer | |
@Before | |
fun setUp() { | |
mockWebServer = MockWebServer() | |
mockWebServer.start() | |
testApis = RetrofitHelper.testApiInstance(mockWebServer.url("/").toString()) | |
repository = UserRepositoryImpl(testApis) |
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
dependencies { | |
... other suppport dependencies | |
// Retrofit | |
implementation 'com.squareup.retrofit2:retrofit:2.9.0' | |
implementation 'com.squareup.retrofit2:converter-gson:2.9.0' | |
// Mock web server | |
testImplementation 'com.squareup.okhttp3:mockwebserver:4.10.0' |
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
data class ApiResponse<T>( | |
val httpCode: Int = HttpURLConnection.HTTP_OK, | |
val body: T? = null, | |
val errorMessage: String? = null, | |
) |
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
interface UserRepository { | |
suspend fun getAllUsers(): ApiResponse<List<User>> | |
suspend fun getUserById(id: Int): ApiResponse<User> | |
} |
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
const val BASE_USER_PATH_URL = "/aqua30/users" | |
interface TestApis { | |
@GET(BASE_USER_PATH_URL) | |
suspend fun getAllUsers(): List<User> | |
@GET("$BASE_USER_PATH_URL/{id}") | |
suspend fun getUserById(@Path("id") id: Int): User | |
} |