Last active
March 7, 2025 13:58
-
-
Save fernandospr/0dca0b4527d723635da242bd4f4fe66d to your computer and use it in GitHub Desktop.
Shows how to mock TextUtils methods so that it's possible to unit test classes that use them
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
import android.text.TextUtils | |
import io.mockk.every | |
import io.mockk.mockkStatic | |
import org.junit.Assert | |
import org.junit.Test | |
class TextValidator { | |
fun validate(string: String) = !TextUtils.isEmpty(string) | |
} | |
class TextValidatorTest { | |
@Test | |
fun test() { | |
mockkStatic(TextUtils::class) | |
every { TextUtils.isEmpty(any()) } answers { arg<String>(0).isEmpty() } | |
val tv = TextValidator() | |
Assert.assertTrue(tv.validate("Fernando")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment