Skip to content

Commit

Permalink
Merge pull request #224 from Geanik/206
Browse files Browse the repository at this point in the history
Add test for SignInUseCaseImpl
  • Loading branch information
hieuwu committed Oct 13, 2023
2 parents 4f528ee + 74c35e1 commit 543b701
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 543b701

Please sign in to comment.