Created
August 19, 2023 15:42
-
-
Save ProArun/644adf84d9ff764e55e4f4635333f52a to your computer and use it in GitHub Desktop.
Unit test Example
This file contains hidden or 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
class PasswordTest{ | |
@Test | |
fun `validatePassword blank input expected required field`(){ | |
val sut = Utils() | |
val result = sut.validatePassword(" ") | |
assertEquals("Password is required field", result) | |
} | |
@Test | |
fun `validatePassword 2CharInput expected validation Msg`(){ | |
val sut = Utils() | |
val result = sut.validatePassword("ab") | |
assertEquals("Length of the password should be greater than 6",result) | |
} | |
@Test | |
fun `validatePassword correctInput expected valid password`(){ | |
val sut = Utils() | |
val result = sut.validatePassword("Pass123") | |
assertEquals("valid",result) | |
} | |
} |
This file contains hidden or 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
class StringTest{ | |
@Test | |
fun `testStringReversal Empty String expected Empty String`(){ | |
val sut = Utils() | |
val result = sut.reverseString(input:"") | |
assertEquals("",result) | |
} | |
@Test | |
fun `testStringReversal Single Char expected Single Char`(){ | |
val sut = Utils() | |
val result = sut.reverseString(input:"a") | |
assertEquals("a",result) | |
} | |
@Test | |
fun `testStringReversal valid input expected same String`(){ | |
val sut = Utils() | |
val result = sut.reverseString(input:"ArunKumar") | |
assertEquals("ramuKnurA",result) | |
} | |
@Test(expected = IllegalArgumentException::class) | |
fun `testStringReversal valid input expected same String`(){ | |
val sut = Utils() | |
val result = sut.reverseString(input:null) | |
} | |
} |
This file contains hidden or 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
class Utils{ | |
fun validatePassword(input: String) = when { | |
input.isBlank() -> { | |
"Password is required field" | |
} | |
input.length < 6 -> { | |
"Length of the password should be greater than 6" | |
} | |
input.length > 15 -> { | |
"Length of the password should be less than 15" | |
} | |
else -> { | |
"Valid" | |
} | |
} | |
fun reverseString(input: String?):String{ | |
if(input == null){ | |
throw IllegalArgumentException("Input String is Required") | |
} | |
var chars = input.toCharArray() | |
var i = 0 | |
var j = chars.size - 1 | |
while(i < j){ | |
val temp = chars[i] | |
chars[i] = chars[j] | |
chars[j] = temp | |
i++ | |
j-- | |
} | |
return chars.joinToString(separator: "") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment