Skip to content

Instantly share code, notes, and snippets.

@mcatta
Created August 20, 2021 08:42
Show Gist options
  • Select an option

  • Save mcatta/967f95d61bd5fe14265abb451f0657b8 to your computer and use it in GitHub Desktop.

Select an option

Save mcatta/967f95d61bd5fe14265abb451f0657b8 to your computer and use it in GitHub Desktop.
package com.marand.domain.user.interactor
import com.marand.domain.MainCoroutineRule
import com.marand.domain.UseCase
import com.marand.domain.factory.user.UsersFactory
import com.marand.domain.user.entity.UserEntity
import com.marand.domain.user.repository.UserRepository
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import junit.framework.Assert.assertNotNull
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.junit.MockitoJUnitRunner
@RunWith(MockitoJUnitRunner::class)
@ExperimentalCoroutinesApi
class GetUserListUseCaseTest {
@get:Rule
var mainCoroutineRule = MainCoroutineRule()
private val userRepository = mockk<UserRepository>()
private lateinit var getUserListUseCase: GetUserListUseCase
@Before
fun setUp() {
getUserListUseCase = GetUserListUseCase(userRepository)
}
@Test
fun `getUserListUseCase calls repository`() = mainCoroutineRule.runBlockingTest {
// Arrange
// No arrangement for this test case
stubUserRepositoryGetUsers(listOf()) // coEvery is mandatory because you need to provide a response to the Mock object
//Act
getUserListUseCase.run(UseCase.None())
//Assert
coVerify { userRepository.getUsers() } // Use coVerify to verify if a method is called
}
@Test
fun `getUserListUseCase, returns data`() = mainCoroutineRule.runBlockingTest {
//Arrange
val userList = UsersFactory.generateDummyUserList(1)
stubUserRepositoryGetUsers(userList)
//Act
val result = getUserListUseCase.run(UseCase.None())
//Assert
assertNotNull(result)
assertEquals("username", result.data[0].username)
assertEquals("name", result.data[0].name)
assertEquals("email", result.data[0].email)
}
/**
* Stub Helpers Methods
*/
private fun CoroutineScope.stubUserRepositoryGetUsers(data: List<UserEntity>) {
coEvery { userRepository.getUsers() } returns data // Use coEvery to mock a response of a method
}
}
@Maryam-T
Copy link

Hi Marco,
This snippet now works for me also. I had to use the "mokk" inside my test code as well. Thanks for your guide.

@mcatta
Copy link
Author

mcatta commented Aug 20, 2021

Cool! You are welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment