Uri.parse()在单元测试中始终返回null [英] Uri.parse() always returns null in unit test
问题描述
这个简单的单元测试总是可以通过,我不知道为什么.
This simple unit test always passes and I can't figure out why.
@RunWith(JUnit4.class)
class SampleTest {
@Test testSomething() {
Uri uri = Uri.parse("myapp://home/payments");
assertTrue(uri == null);
}
}
到目前为止,我尝试使用的是传统" URI( http://example.com
),但是 uri
也是 null
.
What I have tried so far is using a "traditional" URI (http://example.com
) but uri
was also null
.
推荐答案
检查应用的gradle文件中是否包含以下内容:
Check if you have the following in your app's gradle file:
android {
...
testOptions {
unitTests.returnDefaultValues = true
}
Uri
是一个Android类,因此如果没有上面的代码,则不能在本地单元测试中使用:
Uri
is an Android class and as such cannot be used in local unit tests, without the code above you get the following:
java.lang.RuntimeException: Method parse in android.net.Uri not mocked. See http://g.co/androidstudio/not-mocked for details.
上面的代码抑制了此异常,而是提供了返回默认值的虚拟实现(在这种情况下为 null
).
The code above suppresses this exception and instead provides dummy implementations returning default values (null
in this case).
另一种选择是您在测试中使用了一些框架,该框架提供了Android类中方法的实现.
The other option is that you are using some framework in your tests that provides implementations of the methods in Android classes.
这篇关于Uri.parse()在单元测试中始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!