Skip to content

Commit

Permalink
Added Google Truth Library and wrote unit and integrated test
Browse files Browse the repository at this point in the history
Signed-off-by: Ramakrishna Joshi <ramakrishnaj995@gmail.com>
  • Loading branch information
ramakrishnajoshi committed Feb 7, 2021
1 parent a4603f0 commit 2890513
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 7 deletions.
20 changes: 17 additions & 3 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
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()
}
}
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
}
}
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
}
}
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
}
}

0 comments on commit 2890513

Please sign in to comment.