diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 45b5654..3c7772a 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,12 +1,26 @@ - - + + + - \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 85cc99b..276a78d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -30,10 +30,18 @@ dependencies { implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' - //Android Specific Test - androidTestImplementation 'androidx.test.ext:junit:1.1.2' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + //testImplementation includes dependencies only for `test` source-set + //androidTestImplementation includes dependencies only for `androidTest` source-set //JUnit Test testImplementation 'junit:junit:4.13.1' + //Truth gives convenient methods which makes assertions in our test cases much more readable + testImplementation 'com.google.truth:truth:1.0.1' + + //Android Specific Test + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' //espresso is needed for UI Tests + + //If truth dependency is also needed in `androidTest` source-set then we need to add below line + androidTestImplementation 'com.google.truth:truth:1.0.1' } diff --git a/app/src/androidTest/java/com/example/unittestinginandroid/basicunittests/ResourceComparerTest.kt b/app/src/androidTest/java/com/example/unittestinginandroid/basicunittests/ResourceComparerTest.kt new file mode 100644 index 0000000..9712eec --- /dev/null +++ b/app/src/androidTest/java/com/example/unittestinginandroid/basicunittests/ResourceComparerTest.kt @@ -0,0 +1,49 @@ +package com.example.unittestinginandroid.basicunittests + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import com.example.unittestinginandroid.R +import com.google.common.truth.Truth.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test + +class ResourceComparerTest { + + lateinit var resourceComparer: ResourceComparer + + @Before + fun setUp() { + resourceComparer = ResourceComparer() + } + + @After + fun tearDown() { + //no need to do anything here as Garbage Collector will clean up resourceComparer automatically + //but in case if there is DatabaseConection object that that needs to be closed here + } + + @Test + fun shouldReturnFalseIfStringsDontMatch() { + val context = ApplicationProvider.getApplicationContext() + val result = resourceComparer.isEqual( + context, + R.string.app_name, + "SomeRandomString" + ) + + assertThat(result).isFalse() + } + + @Test + fun shouldReturnTrueIfStringsMatch() { + val context = ApplicationProvider.getApplicationContext() + val result = resourceComparer.isEqual( + context, + R.string.app_name, + "UnitTestingInAndroid" + ) + + assertThat(result).isTrue() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/unittestinginandroid/basicunittests/RegistrationUtil.kt b/app/src/main/java/com/example/unittestinginandroid/basicunittests/RegistrationUtil.kt new file mode 100644 index 0000000..a8f02c8 --- /dev/null +++ b/app/src/main/java/com/example/unittestinginandroid/basicunittests/RegistrationUtil.kt @@ -0,0 +1,34 @@ +package com.example.unittestinginandroid.basicunittests + +object RegistrationUtil { + + private val existingUsers = listOf("Ram", "Krishna") + + /* + * input is not valid if... + * ...the userName is empty + * ...the userName is already taken + * ...the password or confirmedPassword is empty + * ...the confirmedPassword is not same as the real password + * ...the password does not contain at least 2 digits + * */ + fun validateRegistrationInput( + userName: String, + password: String, + confirmedPassword: String + ): Boolean { + if (userName.isEmpty() || password.isEmpty() || confirmedPassword.isEmpty()) { + return false + } + if (existingUsers.contains(userName)) { + return false + } + if (password != confirmedPassword) { + return false + } + if (password.count { it.isDigit() } < 2) { + return false + } + return true + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/unittestinginandroid/basicunittests/ResourceComparer.kt b/app/src/main/java/com/example/unittestinginandroid/basicunittests/ResourceComparer.kt new file mode 100644 index 0000000..32a8aa2 --- /dev/null +++ b/app/src/main/java/com/example/unittestinginandroid/basicunittests/ResourceComparer.kt @@ -0,0 +1,10 @@ +package com.example.unittestinginandroid.basicunittests + +import android.content.Context + +class ResourceComparer { + + fun isEqual(context: Context, resId: Int, string: String): Boolean { + return context.getString(resId) == string + } +} \ No newline at end of file diff --git a/app/src/test/java/com/example/unittestinginandroid/basicunittests/RegistrationUtilTest.kt b/app/src/test/java/com/example/unittestinginandroid/basicunittests/RegistrationUtilTest.kt new file mode 100644 index 0000000..84cbfc8 --- /dev/null +++ b/app/src/test/java/com/example/unittestinginandroid/basicunittests/RegistrationUtilTest.kt @@ -0,0 +1,73 @@ +package com.example.unittestinginandroid.basicunittests + +import com.google.common.truth.Truth.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test + +class RegistrationUtilTest { + + lateinit var sut: RegistrationUtil + + @Before + fun setUp() { + sut = RegistrationUtil + } + + @Test + fun `empty userName returns false`() { + val result = sut.validateRegistrationInput( + "", + "1234", + "1234" + ) + + assertThat(result).isFalse() + } + + @Test + fun `should return false if userName is taken`() { + val result = sut.validateRegistrationInput( + "Ram", + "1234", + "1234" + ) + assertThat(result).isFalse() + } + + @Test + fun `should return false if password or confirmedPassword is empty`() { + val result = sut.validateRegistrationInput( + "Sunil", + "", + "" + ) + assertThat(result).isFalse() + } + + @Test + fun `should return false if confirmedPassword is not same as password`() { + val result = sut.validateRegistrationInput( + "Sunil", + "Password@123", + "Password" + ) + assertThat(result).isFalse() + } + + @Test + fun `should return false if password does not contain at least 2 digits`() { + val result = sut.validateRegistrationInput( + "Sunil", + "Password", + "Password" + ) + assertThat(result).isFalse() + } + + @After + fun tearDown() { + //no need to do anything here as Garbage Collector will clean up resourceComparer automatically + //but in case if there is DatabaseConection object that that needs to be closed here + } +} \ No newline at end of file