gpt4 book ai didi

android - Uri.parse() 在单元测试中总是返回 null

转载 作者:IT老高 更新时间:2023-10-28 23:20:13 24 4
gpt4 key购买 nike

这个简单的单元测试总是通过,我不知道为什么。

@RunWith(JUnit4.class)
class SampleTest {
@Test testSomething() {
Uri uri = Uri.parse("myapp://home/payments");
assertTrue(uri == null);
}
}

到目前为止,我尝试的是使用“传统”URI (http://example.com),但 uri 也是 null.

最佳答案

我用 Robolectric 解决了这个问题.

这些是我的单元测试配置

build.gradle

dependencies {
...
testImplementation 'junit:junit:4.12'
testImplementation "org.robolectric:robolectric:3.4.2"
}

测试类

@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
}
}

对我有用,希望对你有帮助。

关于android - Uri.parse() 在单元测试中总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40653625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com