Mockito Uri.parse始终返回null [英] Mockito Uri.parse always returns null
问题描述
我有这种生产方法:
public boolean onShouldOverrideUrlLoading(String url) {
boolean isConsumed = false;
if (url.contains("code=")) {
Uri uri = Uri.parse(url);
String authCode = uri.getQueryParameter("code");
mView.authCodeObtained(authCode);
isConsumed = true;
}
return isConsumed;
}
我有这个Mockito测试方法:
And I have this Mockito test method:
@Test
public void onShouldOverrideUrlLoadingOnAuthCodeObtained(){
String code = "someCode";
boolean isConsumed = mPresenter.onShouldOverrideUrlLoading("http://localhost/?code=" + code);
verify(mView, times(1)).authCodeObtained(code);
assertEquals(isConsumed, true);
}
但是似乎一旦代码运行并到达Uri.parse(url),我就会得到一个空指针.我想念什么?在生产中,这非常完美.仅在测试时,Uri.parse()返回null.
But it seems once the code runs and it reaches Uri.parse(url), I get a null pointer. What am I missing? In production this works perfectly. Only when testing, Uri.parse() returns null.
谢谢!
推荐答案
问题可能出在Uri
类中,因为要测试的代码是静态代码(Uri.parse(...)
),Uri
可能在测试环境.请注意,Android SDK中有两个uri类:
The problem probably lies in Uri
class, as the code being tested is static code (Uri.parse(...)
), Uri
is probably badly initializing in the test environment. Note there's two uri classes in the Android SDK :
我不是Android开发人员,但您可能需要检查测试环境.
I'm not an Android developer, but you may want to check the test environment.
这篇关于Mockito Uri.parse始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!