简体   繁体   English

TextUtils.isEmpty()方法为空字符串返回false

[英]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.

相关问题 TextUtils.isEmpty(null) 返回 false - TextUtils.isEmpty(null) returns false JUnit4 TextUtils.isEmpty()给出与String.isEmpty()不同的结果 - JUnit4 TextUtils.isEmpty() gives different result than String.isEmpty() Kotlin:isNullOrEmpty 与 TextUtils.isEmpty - Kotlin: isNullOrEmpty vs TextUtils.isEmpty 为什么在我已经使用.TextUtils?isEmpty() 时出现“NullPointerException”警告? - Why there is 'NullPointerException' warning when I already use !TextUtils.isEmpty()? Android TextUtils isEmpty vs String.isEmpty - Android TextUtils isEmpty vs String.isEmpty TextUtils.isEmpty(regUserOtherCity.getText()。toString())在按下按钮时不起作用 - TextUtils.isEmpty(regUserOtherCity.getText().toString()) doesnt work on button press 针对URLUtil.isValidUrl()的Android Studio“可能为空”警告,但针对TextUtils.isEmpty()则不 - Android Studio “Might Be Null” Warning for URLUtil.isValidUrl() but not for TextUtils.isEmpty() 数据未添加到 Firebase,数据未通过 Intent 传递且 !TextUtils.isEmpty 不起作用 - Data not being added to Firebase, Data not being passed through Intent and !TextUtils.isEmpty not working 使用PowerMockito.spy时出现错误“未模拟android.text.TextUtils中的方法isEmpty” - Error “Method isEmpty in android.text.TextUtils not mocked” when using PowerMockito.spy java.lang.RuntimeException:android.text.TextUtils 中的方法 isEmpty 未模拟 - java.lang.RuntimeException: Method isEmpty in android.text.TextUtils not mocked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM