0

I have an Base64Util class with amongst others an extension function decodeBase64ToByteArray :

class Base64Util {
  companion object {
    fun String.decodeBase64ToByteArray(): ByteArray {
        return Base64.getUrlDecoder().decode(this)
    }
  }
}

Now I would like to test my Base64Util via Base64IUtilTest. I can access non extension functions of course, but how can I access/test the String.decodeBase64ToByteArray() from Base64UtilTest ?

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
Tranquillo
  • 235
  • 2
  • 13
  • I would omit that `class` and `companion object`, so you basically only have a function inside an empty (or only `import` and `package` statements) `kt`-file – Roland Jan 23 '19 at 10:11

1 Answers1

3

You cannot simply access member extension functions such as decodeBase64ToByteArray from outside. This is only possible if you get into the scope of the object it is defined in:

with(Base64Util.Companion){ //.Companion could be removed
    "123fsad123".decodeBase64ToByteArray()
}

It could make sense to define your util functions as top-level functions in a file base64Utils.kt for example. This way, they can be called in a static way without any issue.

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196