方法为空字符串返回false

方法为空字符串返回false

本文介绍了TextUtils.isEmpty()方法为空字符串返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的测试中返回了false.我想念什么吗?

I have below test which is returning false. Am I missing something?

TextUtils.isEmpty("")

更新:由于某种原因,我无法回答我的问题或添加评论.我正在运行JUNit测试用例,而不是仪器测试用例.正如建议的那样,我发现当我们不作为工具运行时,上述方法返回的值不正确.谢谢大家的帮助.我已对答案和正确的评论进行了投票.

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. 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.

推荐答案

对于空字符串,它应该返回true.来自TextUtils的来源:

It should return true for empty string.From the source of 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);
        }
    });

这篇关于TextUtils.isEmpty()方法为空字符串返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:43