[英]TextUtils.isEmpty() method returning false for an empty string
I have below test which is returning false. 我在测试下面返回假。 Am I missing something?
我想念什么吗?
TextUtils.isEmpty("")
Update: For some reason I am not able to answer to my question or add comment. 更新:由于某种原因,我无法回答我的问题或添加评论。 I am running JUNit test case and not the instrumentation test case.
我正在运行JUNit测试用例,而不是仪器测试用例。 As, suggested I found out that the above method returns incorrect value when we don't run as an Instrumentation.
正如建议的那样,我发现当我们不作为工具运行时,上述方法返回的值不正确。 Thanks every one for help.
谢谢大家的帮助。 I have upvoted the answer and correct comment.
我已对答案和正确的评论进行了投票。
It should return true for empty string. 空字符串应返回true。 From the source of TextUtils:
从TextUtils的来源:
public static boolean isEmpty(@Nullable CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
In tests try using something like: 在测试中,请尝试使用以下方法:
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);
}
});
If you are using Kotlin use isNullOrBlank() or isNullOrEmpty() methods instead. 如果您使用Kotlin,请改用isNullOrBlank()或isNullOrEmpty()方法。 You don't need any mocking for tests.
您不需要任何模拟测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.