Skip to content

Instantly share code, notes, and snippets.

@Sprajapati123
Created July 16, 2025 14:59
Show Gist options
  • Save Sprajapati123/42bbc730f82eb3f52adc0669c44181bf to your computer and use it in GitHub Desktop.
Save Sprajapati123/42bbc730f82eb3f52adc0669c44181bf to your computer and use it in GitHub Desktop.
package com.example.ai36
import com.example.ai36.repository.AuthRepoImpl
import com.example.ai36.repository.UserRepositoryImpl
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.gms.tasks.Task
import com.google.firebase.auth.AuthResult
import com.google.firebase.auth.FirebaseAuth
import junit.framework.TestCase.assertEquals
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers.any
import org.mockito.Captor
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
class AuthUnitTest {
@Mock
private lateinit var mockAuth: FirebaseAuth
@Mock
private lateinit var mockTask: Task<AuthResult>
private lateinit var userRepository: AuthRepoImpl
@Captor
private lateinit var captor: ArgumentCaptor<OnCompleteListener<AuthResult>>
@Before
fun setup() {
MockitoAnnotations.openMocks(this)
userRepository = AuthRepoImpl(mockAuth)
}
@Test
fun testLogin_Successful() {
val email = "[email protected]"
val password = "testPassword"
var expectedResult = "Initial Value" // Define the initial value
// Mocking task to simulate successful registration
`when`(mockTask.isSuccessful).thenReturn(true)
`when`(mockAuth.signInWithEmailAndPassword(any(), any()))
.thenReturn(mockTask)
// Define a callback that updates the expectedResult
val callback = { success: Boolean, message: String? ->
expectedResult = message ?: "Callback message is null"
}
// Call the function under test
userRepository.login(email, password, callback)
verify(mockTask).addOnCompleteListener(captor.capture())
captor.value.onComplete(mockTask)
// Assert the result
assertEquals("Login successfully", expectedResult)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment