0

For testing my business logic I'm using mockk library. But I also have a few helper classes that contain methods that use SpannableString, ForegroundColorSpan etc. I need to write tests for these methods as well. However, I do not quite understand how to write such tests correctly, since before that I wrote tests only for business logic. Can I write for testing a SpannableString test using mockk . Unfortunately, I have not found any good examples to demonstrate how to write such tests. Please help me. Here is example of code for which i need to write tests:

fun formatSum(sum: Double): SpannableString {
    return SpannableString(sum.toString()).apply {
        if (ceil(sum)!= sum) {
            setSpan(
                ForegroundColorSpan(getColor(R.color.red)),
                sum.toString().length - 2,
                sum.toString().length,
                0
            )
        }
    }
}
testivanivan
  • 967
  • 13
  • 36
  • 1
    you should most likely not be testing android libraries to begin with, it is usually safe to assume they work, quite hard to answer you without any code though – a_local_nobody Dec 28 '21 at 08:39
  • @a_local_nobody, Please see the changes, I added a piece of code for which I need to write tests – testivanivan Dec 28 '21 at 09:04
  • 2
    i'd say that `(ceil(sum)!= sum)` is still business logic you can move out to a different method, which you can write a standard test for. then, the start and end index and the rest of the span logic you could write methods for as well, even if you just make a method which returns 0, at least you can write a test for that, then you can write standard tests for those, ie: `assert value is 0` type of test, then you don't have to really test the span at all. at least that's how i would approach this – a_local_nobody Dec 28 '21 at 09:24
  • or.... use Robolectric which is simply overkill for just testing this. But if you really want to test android SDK, robolectric is one of the ways to go. – Shark Dec 28 '21 at 10:50

0 Answers0