7

What am I missing? This doesn't make any sense:

screenshot of Android Studio

Why is TextUtils.isEmpty(null) returning false?

tir38
  • 9,810
  • 10
  • 64
  • 107

2 Answers2

7

I faced this same issue while writing my unit test. The problem was I had following setting in my Gradle:

testOptions {
    unitTests.returnDefaultValues = true
}

By default android code returned default value, while running my unit tests. Just get rid of that and you should be fine.

LoveForDroid
  • 1,072
  • 12
  • 25
  • Ahh thanks. Looks like Android team is already aware of this "We are aware that the default behavior is problematic when using classes like Log or TextUtils and will evaluate possible solutions in future releases." http://tools.android.com/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.- thx – tir38 Oct 05 '16 at 19:44
  • Thanks, was getting furious at this odd (or so I thought) behaviour! – user3264740 Dec 14 '17 at 02:31
  • 2023 here, and this behavior is still gaslighting programmers into thinking we've gone mad XD – CCJ Mar 21 '23 at 23:36
0

If you are Using Kotlin, You can replace

TextUtils.isEmpty()

with

isNullOrEmpty

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/is-null-or-empty.html

Eggcellentos
  • 1,570
  • 1
  • 18
  • 25