Test Driven Development
MainPrinciple:Write the test case before the implementation of the function (only for unit tests)
Write the function signature
Write the test cases for the function
Write the function logic so the tests pass
You should only have one assertion per test case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 object RegistrationUtil { private val exitingUsers = listOf("Peter" ,"Carl" ) fun validateRegistrationInput ( userName: String , password: String , confirmedPassword: String ) :Boolean { if (userName.isEmpty()||password.isEmpty()){ return false } if (userName in exitingUsers){ return false } if (password!=confirmedPassword){ return false } if (password.count{it.isDigit()}<2 ){ return false } return true } }
RegistrationUtilTest.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 import com.google.common.truth.Truth.assertThatimport org.junit.Testclass RegistrationUtilTest { @Test fun `empty username returns false `() { val result = RegistrationUtil.validateRegistrationInput( "" , "123" , "123" ) assertThat(result).isFalse() } @Test fun `valid username and correctly repeated password returns true `() { val result = RegistrationUtil.validateRegistrationInput( "Philipp" , "123" , "123" ) assertThat(result).isTrue() } @Test fun `username already exists returns false `() { val result = RegistrationUtil.validateRegistrationInput( "Carl" , "123" , "123" ) assertThat(result).isFalse() } @Test fun `incorrectly confirmed password returns false `() { val result = RegistrationUtil.validateRegistrationInput( "Philipp" , "123456" , "abcdefg" ) assertThat(result).isFalse() } @Test fun `empty password returns false `() { val result = RegistrationUtil.validateRegistrationInput( "Philipp" , "" , "" ) assertThat(result).isFalse() } @Test fun `less than 2 digit password returns false `() { val result = RegistrationUtil.validateRegistrationInput( "Philipp" , "abcdefg5" , "abcdefg5" ) assertThat(result).isFalse() } }
https://www.youtube.com/watch?v=W0ag98EDhGc&list=PLQkwcJG4YTCSYJ13G4kVIJ10X5zisB2Lq&index=3
https://developer.android.com/kotlin/coroutines/coroutines-best-practices