Uri.parse() always returns null in unit test

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)