Unit Testing Login Functionality in Android with Kotlin and Mockito
Step 1: The LoginViewModel Class
First, let’s define our LoginViewModel class. This class contains a login function that takes a username and password as input and returns a Boolean indicating whether the login was successful.
class LoginViewModel {
fun login(username: String, password: String): Boolean {
// Login logic here
}
}
Step 2: Writing the Test Class
Next, we’ll write a test class for LoginViewModel. We’ll call it LoginViewModelTest. In this class, we’ll write test cases to verify the behavior of the login function.
class LoginViewModelTest {
private lateinit var loginViewModel: LoginViewModel
@Before
fun setup() {
loginViewModel = LoginViewModel()
}
@Test
fun `login with valid credentials returns true`() {
val username = "testUser"
val password = "testPassword"
val result = loginViewModel.login(username, password)
assertTrue(result)
}
@Test
fun `login with invalid credentials returns false`() {
val username = "wrongUser"
val password = "wrongPassword"
val result = loginViewModel.login(username, password)
assertFalse(result)
}
}
In this test class, we have two test cases:
login with valid credentials returns true: This test verifies that the login function returns true when provided with valid credentials.
login with invalid credentials returns false: This test verifies that the login function returns false when provided with invalid credentials.
That’s it! You’ve now written unit tests for the login functionality of an Android application using Kotlin and Mockito. Remember to run your tests regularly to catch any bugs or regressions in your code. Happy testing!