Skip to content

Instantly share code, notes, and snippets.

@fernandospr
Last active May 31, 2022 06:42
Show Gist options
  • Save fernandospr/0dca0b4527d723635da242bd4f4fe66d to your computer and use it in GitHub Desktop.
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
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