https://kotlinlang.org logo
Title
t

Timur Atakishiev

12/05/2020, 10:42 AM
hi guys, I have a function that looks like below
interface StorageService {
    fun upload(inputStream: InputStream): String
}

class LocalStorageService: StorageService {

    override upload(inputStream: InputStream): String {
        //Some uploading logic
    }
}

I want to verify that the function was called with the following code

verify {
    storageService.putFile(
        inputStream = withArg { assertEquals("Hello, World!".toByteArray(),it.readBytes())}
    )
}
and I got an error, that my inputStreams byteArray is empty, can anyone help me please
m

Mattia Tommasone

12/06/2020, 9:58 PM
i think what you want to do can be achieved by doing
verify {
    storageService.putFile(match {
        it.readBytes() == "Hello, World!".toByteArray()
    })
}
or, in an even clearer way, by using a slot
val inputStreamSlot = slot<InputStream>()
verify {
    storageService.putFile(capture(inputStreamSlot))
}

assertEquals("Hello, World!".toByteArray(), inputStreamSlot.captured.readBytes())
t

Timur Atakishiev

12/07/2020, 5:37 AM
Thank you for an answer, but both of the variants are not working. In the first one tells that the bytes of
Hello, World!
and bytes of an inputStream are not equal. Second one(with the
slot
) tells that the
inputStreamSlot.captured.readBytes
is empty
When I am using mock
every { storageService.putFile(match{}) }
matcher is working fine, however, in verify section, it is not working. Strange
finally, I do something like
verify {storageService.upload(inputStream = matcher{it.reset and then it.readBytes() contentEquals "Hello, wordl".toByteArray()}
What do you think is it okay variant?