Skip to content

Instantly share code, notes, and snippets.

@mustafayigitt
Last active October 4, 2021 06:56
Embed
What would you like to do?
package com.packagename.data.repository
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.packagename.BuildConfig
import com.packagename.R
import com.packagename.data.local.AppDatabase
import com.packagename.data.model.AuthModel
import com.packagename.data.preferences.PreferencesManager
import com.packagename.data.remote.response.ResponseRegister
import com.packagename.data.remote.service.AuthService
import com.packagename.util.ResponseWrapper
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.impl.annotations.MockK
import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import retrofit2.Response
@RunWith(JUnit4::class)
class AuthRepositoryTest : TestCase() {
@MockK
private lateinit var remote: AuthService
@MockK
private lateinit var database: AppDatabase
@MockK
private lateinit var prefs: PreferencesManager
@MockK
private lateinit var googleClient: GoogleSignInClient
private lateinit var authRepository: AuthRepository
private val validAuthModel = AuthModel(
"google",
"${System.currentTimeMillis()}@gmail.com",
"123456789",
BuildConfig.CLIENT_ID,
BuildConfig.CLIENT_SECRET,
BuildConfig.AUTHENTICATED_USER_ID,
"Build.ID", "Build.MODEL", BuildConfig.PLATFORM
)
private val invalidAuthModel = AuthModel(
"google",
"abc@gmail.com",
"123456789",
BuildConfig.CLIENT_ID,
BuildConfig.CLIENT_SECRET,
BuildConfig.AUTHENTICATED_USER_ID,
"Build.ID", "Build.MODEL", BuildConfig.PLATFORM
)
private val successResponseModel = ResponseRegister(
"uuid",
"token",
"refresh",
"expiration",
"success", "200", null
)
@Before
public override fun setUp() {
MockKAnnotations.init(this, relaxUnitFun = true)
authRepository = AuthRepository(remote, database, prefs, googleClient)
coEvery {
remote.register(validAuthModel)
} returns Response.success(successResponseModel)
coEvery {
remote.register(invalidAuthModel)
} returns Response.error(400, "USER_EXIST".toResponseBody())
}
@Test
fun `when register return success`() {
runBlocking {
val result = authRepository.register(validAuthModel)
assertEquals(true, result is ResponseWrapper.Success)
}
}
@Test
fun `when register return fail`() {
runBlocking {
val result = authRepository.register(invalidAuthModel)
assertEquals(R.string.error_generic_message, (result as ResponseWrapper.Error).errorId)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment