Search code examples
androidunit-testingandroid-testinggoogle-truth

Using Google Truth with Android text views


This may be obvious, but I can't seem to find a better way to handle this. I'm switching my project from using AssertJ to using Truth for several reasons that are beyond the scope of this question. In AssertJ, I could work easily with Android "string-like" classes. For instance, this was valid in AssertJ:

assertThat(mEmailEditText.getText()).isEmpty();

But in Truth, it looks like the core Truth library doesn't recognize how to convert from Editable/CharSequence into String. That means I have to write a lot of the following:

assertThat((String) mEmailEditText.getText()).isEmpty();

...which kind of defeats the purpose of using Truth. I suspect I could write my own custom subject for CharSequence, but I was curious if there was a more accepted practice for this.


Solution

  • You can use

    assertThat(mEmailEditText.getText().toString()).isEmpty();