Unit Testing Series #3
In this article, I am going to explore the building blocks to understand how to enter into testing world of Android app.
Annotations that are building Blocks of Unit test class
- @Test : This is the annotation that will make any method to be run under Junit. Once you add @Test annotation you will be able to test any existing functionality.
- @Before : This is the annotation that will be used for pre-requisite for any unit test methods. This method will be executed before executing any @test method of any test class. It is recommended to use @Before method when you have scenario’s like a particular class instance is going to reuse in many test methods then initialize it in @Before method will be a best choice.
- @After : This is the annotation that will be used after all the test method of a test class. @After annotation method can be used for tear down feature like shutdown feature. This is also very important to keep your variables reset once unit testing done.
Steps to test a simple feature in android
Step 1 : Write your feature class like I have a feature for register a user.
object ValidateRegistrationInput {
private val existingUsers = listOf("Abhishek", "Kai", "Jason")
fun isValidRegistrationInput(
username: String,
password: String,
confirmPassword: String
): Boolean {
if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
return false
}
if (username in existingUsers) {
return false
}
if (password != confirmPassword) {
return false
}
if (password.count { it.isDigit() } < 6) {
return false
}
return true
}
}
Step 2: Let’s have a testing class so for that right click on the existing feature class and click on generate.
Step 3 : Click on Test that will ask you next steps
Step 4: After that a pop up came that will show a suggestion of your test class and few methods options to generate like @Before and @After. Click Ok.
Step 5: Now you got another popup that is for picking folder for test. By default all the local test should be tested under test folder while UI & Integration test should be created under androidTest folder.
Step 6: Write all the test cases required to test the feature end to end
internal class ValidateRegistrationInputTest {
@Test
fun `When username is empty Then return false`() {
val output = ValidateRegistrationInput.isValidRegistrationInput(
EMPTY_STRING,
TEST_PASSWORD,
TEST_CONFIRMED_PASSWORD
)
assertFalse(output)
}
@Test
fun `When username, password, confirmedPassword is correct Then return true`() {
val output = ValidateRegistrationInput.isValidRegistrationInput(
TEST_USERNAME,
TEST_PASSWORD,
TEST_CONFIRMED_PASSWORD
)
assertTrue(output)
}
@Test
fun `When username already exists Then return false`() {
val output = ValidateRegistrationInput.isValidRegistrationInput(
TEST_EXISTING_USERNAME,
TEST_PASSWORD,
TEST_CONFIRMED_PASSWORD
)
assertFalse(output)
}
@Test
fun `When wrong confirmPassword entered Then return false`() {
val output = ValidateRegistrationInput.isValidRegistrationInput(
TEST_USERNAME,
TEST_PASSWORD,
TEST_WRONG_CONFIRMED_PASSWORD
)
assertFalse(output)
}
@Test
fun `When Password is less than six char Then return false`() {
val output = ValidateRegistrationInput.isValidRegistrationInput(
TEST_USERNAME,
TEST_INVALID_PASSWORD,
TEST_CONFIRMED_PASSWORD
)
assertFalse(output)
}
@Before
fun setUp() {
//This is the method where you can initialize the variable and use them for all test methods.
}
@After
fun tearDown() {
// this method is used to unMock all the used variable or used as tear down feature
}
private companion object {
const val TEST_USERNAME = "test"
const val TEST_PASSWORD = "12345678"
const val TEST_INVALID_PASSWORD = "1234"
const val TEST_CONFIRMED_PASSWORD = "12345678"
const val TEST_WRONG_CONFIRMED_PASSWORD = "123456789"
const val EMPTY_STRING = ""
const val TEST_EXISTING_USERNAME = "Abhishek"
}
}
Step 7 : Run the Test cases in one go or you can also run each test cases.
Unit Testing Series
- Basics of Unit Testing
- TDD vs BDD methodology
- UI Testing with Espresso in Android
- Integration testing in Android (coming soon)
- MVVM viewmodel testing in Android (coming soon)
Thank you for reading my article. I really appreciate 👏 your response.
Clap if this article helps you. It means a lot to me. If I got something wrong, please comment for improve.
let’s connect on Linkedin , GitHub