Unit testing on Android for Functions and Methods | Erselan Khan
In this article, we will write some unit tests for Functions and Methods. We will follow the Test-driven development (TDD) rule for this article. TDD is a software development process that relies on software requirements being converted to test cases before the software is fully developed, and tracks all software development by repeatedly testing the software against all test cases.

Test-Driven Development rules are listed below:
- Write a failing test
- Make the test pass
- Refactor
So let’s start with the dependencies for Unit Testing. Open your Gradle file and add these dependencies:
testImplementation "com.google.truth:truth:1.1.3"
testImplementation 'junit:junit:4.13.2'
In this article, we will test the functionality of the isValidLoginFields function inside our FieldValidation class.
object FieldValidation {
private const val EMAIL_PATTERN = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"
fun isValidLoginFields(email: String, password: String): Boolean {
return true
}
}
As we are following the TDD rule, we will write test cases first and define the function’s body later.
We have two types of Test packages in Android:
- Simple Test -> These tests are not dependent on Android Components.
- Android Test -> These tests are dependent on Android Components like Context.

Since we are testing methods and functions, which are not dependent on Android Components, we create these tests in the test package.
Let’s write down some cases for valid and invalid inputs:
- If the email field is empty, it will return false.
- If the password field is empty, it will return false.
- If both email and password fields are empty, will return false.
- If the email field is valid but the password is less than six characters, it will return false.
- If the email field is invalid and the password is valid, it will return false.
- If both email and password are valid, it will return true.
Right-click on FieldValidation class to create a test class and select Generate → Test.

Press OK and select test package and press OK again.


Android Studio will create a class i.e FieldValidationTest. Now write test cases for all six conditions which we have discussed above and pass the parameters respectively.
import com.google.common.truth.Truth.assertThat
import org.junit.Test
class FieldValidationTest {
/*
test case for empty email address
*/
@Test
fun `empty email address returns false`() {
val resultValue = FieldValidation.isValidLoginFields(
"",
"some password"
)
assertThat(resultValue).isFalse()
}
/*
test case for empty password
*/
@Test
fun `empty password returns false`() {
val resultValue = FieldValidation.isValidLoginFields(
"arsalankhan994@gmail.com",
""
)
assertThat(resultValue).isFalse()
}
/*
test case for empty email address and password
*/
@Test
fun `empty email address and password returns false`() {
val resultValue = FieldValidation.isValidLoginFields(
"",
""
)
assertThat(resultValue).isFalse()
}
/*
test case for valid email and invalid password (less than 6 characters)
*/
@Test
fun `valid email address and password is less than 6 characters returns false`() {
val resultValue = FieldValidation.isValidLoginFields(
"arsalankhan994@gmail.com",
"1234"
)
assertThat(resultValue).isFalse()
}
/*
test case for invalid email and valid password
*/
@Test
fun `invalid email address and valid password returns false`() {
val resultValue = FieldValidation.isValidLoginFields(
"arsalankhan994",
"123456"
)
assertThat(resultValue).isFalse()
}
/*
test case for both valid fields
*/
@Test
fun `valid email address and password returns true`() {
val resultValue = FieldValidation.isValidLoginFields(
"arsalankhan994@gmail.com",
"123456"
)
assertThat(resultValue).isTrue()
}
}
We are using @Test annotation on every method which we want to test and using the assertThat method from the Truth Library to compare the value of the result. Now run these test cases and check the results.

Most test cases failed because the definition of the isValidLoginFields method is still missing. Therefore, write the definition of the method to validate fields and return respectively.
object FieldValidation {
private const val EMAIL_PATTERN = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"
/*
return false if
email is empty or
password is empty or
password is not greater than 6 characters and email is not valid
*/
fun isValidLoginFields(email: String, password: String): Boolean {
if (email.isEmpty()) return false
if (password.isEmpty()) return false
return password.length >= 6 && email.matches(EMAIL_PATTERN.toRegex())
}
}
Now run these test cases again and check the results:

In the end, all test cases passed and we are good to go with the method. I will cover more test cases in my next article.
Show your love ❤️ by sharing this article with your fellow developers 😅 or Buy me a pizza by clicking this link 😅 https://www.buymeacoffee.com/erselankhan.
In case you missed:
- https://erselankhan.medium.com/junit-annotations-android-f4983ca7d581
- https://erselankhan.medium.com/unit-testing-androids-room-database-erselan-khan-a22227823ede
(Again, the source for this project is on https://github.com/arsalankhan994/unit-testing-step-by-step/tree/methods-or-functions-testing. If you have any questions, go ahead and ask and I’ll do my best to respond.)