Skip to content

Commit

Permalink
Add test for SignInUseCaseImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
Geanik committed Oct 12, 2023
1 parent 130c13b commit 74c35e1
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.hieuwu.groceriesstore.domain.usecases.impl

import com.hieuwu.groceriesstore.data.repository.UserRepository
import com.hieuwu.groceriesstore.domain.usecases.SignInUseCase
import java.util.UUID
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import junit.framework.TestCase.assertEquals
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.whenever
import kotlin.random.Random

@RunWith(MockitoJUnitRunner::class)
class SignInUseCaseImplTest {

@Mock
lateinit var mockedUserRepository: UserRepository
private lateinit var testee: SignInUseCase

@Before
fun setUp() {
testee = SignInUseCaseImpl(
userRepository = mockedUserRepository
)
}

@Test
fun whenExecute_withoutException_thenReturnCorrectValue() {
val input = SignInUseCase.Input(
email = UUID.randomUUID().toString(),
password = UUID.randomUUID().toString()
)
val authenticationResult = Random.nextBoolean()

runBlocking {
whenever(mockedUserRepository.authenticate(input.email, input.password))
.thenReturn(authenticationResult)

val output = testee.execute(input)
assertEquals(authenticationResult, output.result)
}
}

@Test
fun whenExecute_withException_thenReturnAccountNotExistedError() {
val input = SignInUseCase.Input(
email = UUID.randomUUID().toString(),
password = UUID.randomUUID().toString()
)

runBlocking {
whenever(mockedUserRepository.authenticate(input.email, input.password))
.thenThrow(RuntimeException())

val output = testee.execute(input)
assertEquals(SignInUseCase.Output.AccountNotExistedError, output)
}
}
}

0 comments on commit 74c35e1

Please sign in to comment.