Created
January 25, 2026 02:46
-
-
Save Sprajapati123/d1ee7586dea2fd24cbc2899c38a2865b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.ai37c | |
| import com.example.ai37c.repository.UserRepo | |
| import com.example.ai37c.viewmodel.UserViewModel | |
| import org.junit.Assert.assertEquals | |
| import org.junit.Assert.assertTrue | |
| import org.junit.Test | |
| import org.mockito.kotlin.any | |
| import org.mockito.kotlin.eq | |
| import org.mockito.kotlin.doAnswer | |
| import org.mockito.kotlin.mock | |
| import org.mockito.kotlin.verify | |
| class UserViewModelTest { | |
| @Test | |
| fun login_success_test() { | |
| val repo = mock<UserRepo>() | |
| val viewModel = UserViewModel(repo) | |
| doAnswer { invocation -> | |
| val callback = invocation.getArgument<(Boolean, String) -> Unit>(2) | |
| callback(true, "Login success") | |
| null | |
| }.`when`(repo).login(eq("test@gmail.com"), eq("123456"), any()) | |
| var successResult = false | |
| var messageResult = "" | |
| viewModel.login("test@gmail.com", "123456") { success, msg -> | |
| successResult = success | |
| messageResult = msg | |
| } | |
| assertTrue(successResult) | |
| assertEquals("Login success", messageResult) | |
| verify(repo).login(eq("test@gmail.com"), eq("123456"), any()) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment