我有下面的测试,它返回false。我是不是遗漏了什么?
TextUtils.isEmpty("")更新:由于某些原因,我无法回答我的问题或添加评论。我运行的是JUNit测试用例,而不是插装测试用例。正如我所建议的那样,我发现当我们不作为Instrumentation运行时,上面的方法返回不正确的值。感谢每一个人的帮助。我已经对答案和正确的评论进行了提升。
发布于 2016-06-07 03:01:19
对于空字符串,它应该返回true。来自TextUtils的源代码:
public static boolean isEmpty(@Nullable CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}在测试中,尝试使用以下内容:
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);
}
});发布于 2019-03-13 18:37:10
如果您使用的是Kotlin,请改用isNullOrBlank()或isNullOrEmpty()方法。你不需要为测试做任何模拟。
https://stackoverflow.com/questions/37664553
复制相似问题