Skip to content

Commit

Permalink
implement GetShipsUseCase the test case passed
Browse files Browse the repository at this point in the history
  • Loading branch information
mutukuian committed May 13, 2024
1 parent 23f607b commit f20d59e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {
//implementation(libs.androidx.paging.compose)
// implementation(libs.androidx.material3.android)
testImplementation(libs.junit)
testImplementation("junit:junit:4.12")
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
Expand Down Expand Up @@ -118,6 +119,18 @@ dependencies {
kapt("androidx.room:room-compiler:2.6.1")
implementation("androidx.room:room-paging:2.6.1")


testImplementation("junit:junit:4.13")
testImplementation("org.mockito:mockito-core:5.2.1")
testImplementation("org.mockito.kotlin:mockito-kotlin:5.2.1")

//truth
testImplementation ("com.google.truth:truth:1.0.1")





}
//allow references to generate code
kapt{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.example.kocelainterview.domain.use_case.get_ships

import com.example.kocelainterview.common.core.Resource
import com.example.kocelainterview.data.remote.dto.ShipDto
import com.example.kocelainterview.data.remote.dto.toShip
import com.example.kocelainterview.domain.model.Ship
import com.example.kocelainterview.domain.repository_interface.ShipRepository
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import org.junit.Assert.*
import org.junit.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when`

class GetShipsUseCaseTest {

@Test
fun `test invoke`() = runBlocking {
// Mock repository
val mockRepository = mock(ShipRepository::class.java)

// Mock response from repository
val mockShipDtos = listOf(
ShipDto(
abs = 123,
active = true,
attempted_landings = 2,
`class` = 1,
course_deg = 45.0,
home_port = "Port 1",
image = "https://example.com/image1.jpg",
imo = 456,
mmsi = 789,
roles = listOf("Role 1", "Role 2"),
ship_id = "ship_id_1",
ship_model = "Model 1",
ship_name = "Ship 1",
ship_type = "Type 1",
speed_kn = 20,
status = "Status 1",
successful_landings = 1,
url = "https://example.com/ship1",
weight_kg = 1000,
weight_lbs = 2200,
year_built = 2020
)

)
`when`(mockRepository.getShips()).thenReturn(mockShipDtos)

// Create an instance of GetShipsUseCase with the mocked repository
val useCase = GetShipsUseCase(mockRepository)

// Invoke the use case within a coroutine
val result = useCase().toList()


// Verify loading state
assert(result[0] is Resource.Loading)

// Verify success state
val expectedSuccess = Resource.Success(mockShipDtos.map { it.toShip() })
assert(result[1] is Resource.Success)
assertEquals(expectedSuccess.data, (result[1] as Resource.Success).data)


}
}

0 comments on commit f20d59e

Please sign in to comment.