From 74c35e1290163491a1002828cd7adbfa5c46ccfe Mon Sep 17 00:00:00 2001 From: Geanik Date: Thu, 12 Oct 2023 18:44:46 +0200 Subject: [PATCH] Add test for SignInUseCaseImpl --- .../usecases/impl/SignInUseCaseImplTest.kt | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/SignInUseCaseImplTest.kt diff --git a/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/SignInUseCaseImplTest.kt b/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/SignInUseCaseImplTest.kt new file mode 100644 index 00000000..62533983 --- /dev/null +++ b/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/SignInUseCaseImplTest.kt @@ -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) + } + } +} \ No newline at end of file