Issue
I'm trying to test my use case through Mockito, but it doens't seem to work very well. This is my use case that I wanna test:
class GetStudentsUseCase @Inject constructor(private val studentRepository: StudentRepository) {
suspend fun execute():Result<List<Student>> = studentRepository.getStudents()
}
Then, I created GetStudentsUseCaseTest that looks like this:
class GetStudentsUseCaseTest {
private lateinit var cut: GetStudentsUseCase
private var reservationList = listOf(
Student("Max", "122d"),
Student("Steven", "012s")
)
@Before
fun setUp() {
val fakeStudentRepository = Mockito.mock(StudentRepository::class.java)
cut = GetStudentsUseCase(fakeStudentRepository)
}
@Test
suspend fun test_name() {
Mockito.`when`(cut.execute()).thenReturn(Result.success(reservationList))
}
}
What should I put inside the test_name method?
Edit: answer with solution
Solution
@OptIn(ExperimentalCoroutinesApi::class)
class GetStudentsUseCaseTest {
private lateinit var cut: GetStudentsUseCase
@Mock
private lateinit var repository: StudentRepository
private var reservationList = listOf(
Student("Max", "122d"),
Student("Steven", "012s")
)
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
cut = GetStudentsUseCase(repository)
}
@Test
fun test_name() = runTest {
Mockito.`when`(repository.getStudents()).thenReturn(Result.success(reservationList))
val result = cut.execute()
assertEquals(result, Result.success(reservationList))
}
}
Answered By - Jonathan
Answer Checked By - Mildred Charles (JavaFixing Admin)