-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Google Truth Library and wrote unit and integrated test
Signed-off-by: Ramakrishna Joshi <ramakrishnaj995@gmail.com>
- Loading branch information
1 parent
a4603f0
commit 2890513
Showing
8 changed files
with
195 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
49 changes: 49 additions & 0 deletions
49
.../androidTest/java/com/example/unittestinginandroid/basicunittests/ResourceComparerTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Context>() | ||
val result = resourceComparer.isEqual( | ||
context, | ||
R.string.app_name, | ||
"SomeRandomString" | ||
) | ||
|
||
assertThat(result).isFalse() | ||
} | ||
|
||
@Test | ||
fun shouldReturnTrueIfStringsMatch() { | ||
val context = ApplicationProvider.getApplicationContext<Context>() | ||
val result = resourceComparer.isEqual( | ||
context, | ||
R.string.app_name, | ||
"UnitTestingInAndroid" | ||
) | ||
|
||
assertThat(result).isTrue() | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/example/unittestinginandroid/basicunittests/RegistrationUtil.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.example.unittestinginandroid.basicunittests | ||
|
||
object RegistrationUtil { | ||
|
||
private val existingUsers = listOf<String>("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 | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/example/unittestinginandroid/basicunittests/ResourceComparer.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
app/src/test/java/com/example/unittestinginandroid/basicunittests/RegistrationUtilTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |