我有以下测试,它返回false.我错过了什么吗?
TextUtils.isEmpty("")
Run Code Online (Sandbox Code Playgroud)
更新:由于某种原因,我无法回答我的问题或添加评论.我正在运行JUNit测试用例而不是仪器测试用例.因为,我建议我发现当我们不作为Instrumentation运行时,上面的方法返回不正确的值.谢谢大家的帮助.我赞成了答案并正确评论.
对于空字符串,它应该返回 true。来自 TextUtils 的来源:
public static boolean isEmpty(@Nullable CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
在测试中尝试使用类似的东西:
mockStatic(TextUtils.class);
when(TextUtils.isEmpty(any(CharSequence.class))).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
String string = (String) args[0];
return (string == null || string.length() == 0);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2048 次 |
| 最近记录: |