Skip to content

Instantly share code, notes, and snippets.

View Senemix29's full-sized avatar
🏠
Working from home

Natan Ximenes Senemix29

🏠
Working from home
View GitHub Profile
@Senemix29
Senemix29 / NameValidatorTest.kt
Last active August 7, 2021 23:51
A test class to be used as an example at a medium post about testing
class NameValidatorTest {
private val nameValidator = NameValidator()
@Test
fun 'isValid should return false when name is empty'() {
val name = ""
assertEquals(false, nameValidator.isValid(name))
}
@Test
@Senemix29
Senemix29 / NameValidator.kt
Created August 7, 2021 23:47
A NameValidator class to be used as and example at a Medium post about testing
private const val MIN_NAME_LENGTH = 3
class NameValidator {
fun isValid(name: String): Boolean {
return when {
name.isEmpty() -> false
name.length < MIN_NAME_LENGTH -> false
else -> true
}
}
}
@Senemix29
Senemix29 / FileUtilsTest.kt
Created March 18, 2021 03:02
Dummy example class to be shown embed at Medium
class FileUtils {
fun getDirectoryFilesListSize(context: Context, fileDirectory: String): Int {
val directory = File(context.filesDir, fileDirectory)
val fileList = directory.listFiles().orEmpty()
return fileList.size
}
}
@Senemix29
Senemix29 / FileUtilsTest.kt
Created March 18, 2021 03:00
Example of Android Instrumented Unit Test Using Espresso
@Test
fun shouldReturnNumberOfFilesInsideADirectory() {
//Given
val bananaDirectory = "bananas"
createFileAtInternalDirectory(context, bananaDirectory, "prata")
createFileAtInternalDirectory(context, bananaDirectory, "d'agua")
createFileAtInternalDirectory(context, bananaDirectory, "nanica")
//When
val directoryFileListSize =
@Senemix29
Senemix29 / FightClubActivityTest.kt
Last active March 18, 2021 03:03
Example of Android Instrumented tests using Espresso
@Test
fun shouldEnableButtonWhenValidMessageIsTyped() {
onView(withId(R.id.messageField)).perform(typeText("you do not talk about Fight Club"))
onView(withId(R.id.sendButton)).check(matches(isEnabled()))
}
@Test
fun shouldNotEnableButtonWhenInvalidMessageIsTyped() {
onView(withId(R.id.messageField)).perform(typeText("Hey, let me tell you about the Fight Club"))
onView(withId(R.id.sendButton)).check(matches(not(isEnabled())))