I resolve this problem with Robolectric.
these are my unit test config
build.gradle
dependencies {
...
testImplementation 'junit:junit:4.12'
testImplementation "org.robolectric:robolectric:3.4.2"
}
test class
@RunWith(RobolectricTestRunner.class)
public class TestClass {
@Test
public void testMethod() {
Uri uri = Uri.parse("anyString")
//then do what you want, just like normal coding
}
}
kotlin
@RunWith(RobolectricTestRunner::class)
class TestClass {
@Test
fun testFunction() {
val uri = Uri.parse("anyString")
//then do what you want, just like normal coding
}
}
it works for me, hope this can help you.