I created a validation use case in which I'm validating the input using isDigitsOnly that use TextUtils internally.
override fun isDigitsOnly(size: String): Boolean {
return !size.trim().isDigitsOnly()
}
when I tried to test it, I got this error
Method isDigitsOnly in android.text.TextUtils not mocked
Does anyone know how I can mock the textUtils in my test class
@RunWith(MockitoJUnitRunner::class)
class ValidationInputImplTest {
@Mock
private lateinit var mMockTextUtils: TextUtils
private lateinit var validationInputImpl: ValidationInputImpl
@Before
fun setUp() {
validationInputImpl = ValidationInputImpl()
}
@Test
fun `contains only digits, returns success`() {
val input = "66"
val result = validationInputImpl(input)
assertTrue(result is ValidationResult.Success)
}
}
At the time of writing the answer,you cannot do that. Because the android code ,that you see is not actual code. You cannot create unit test for that. The default implementation of the methods in
android.jar
is tothrow exception
.One thing you can do is, adding the below in build.gradle file. But it make the android classes not to throw exception. But it will always return default value. So the test result may actually not work. It is strictly not recommended.
The better way to do copy the code from android source and paste the file under
src/test/java
folder with package name asandroid.text
.Link to Answer