mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.1k stars 200 forks source link

Mocking with amazon s3 #407

Closed thaibt closed 3 years ago

thaibt commented 3 years ago

Implementation try { var s3Object: S3Object = s3Client.getObject(getObjectRequest) s3Object.use { s3Object -> s3Object.getObjectContent().use { objectContent -> BufferedInputStream(objectContent).use { bufferedInputStreams -> bytes = bufferedInputStreams.readAllBytes() } } } } catch (e: IOException) { throw InternalException(e.message, e) } catch (e: IllegalArgumentException) { val message = "Invalid input" throw InternalException(message, e)}

Test Code @Test funget object no errors() { val s3Object = S3Object() s3Object.setObjectContent(ByteArrayInputStream("text".toByteArray())) s3 = mock<AmazonS3> { on { getObject(any(GetObjectRequest::class.java)) } doReturn s3Object } val byteBuffer: ByteArray? = s3ClientWrapper.getObject(S3_KEY) assertNotNull(byteBuffer) }

I am trying to mock with the S3AmazonClient but I am encountering a weird issue. My code for testing putObject in the native AmazonS3 code base tested just fine, but for this one - the mock gets intercept but it is not returning what I need it to? Am I missing something or is this a bug? It just jumps to the end of the implementation and the object that was supposed to get set bytes is still null from the initialization

thaibt commented 3 years ago

Hmm,

So the issue seems to be the the fact that I overwrite the s3 mock and it's being overwritten but the wrapper is not picking up the new when cause that I added

on { getObject(any(GetObjectRequest::class.java)) } doReturn s3Object

Am I missing an idea where I can extend that same S3 instead of instantiating the mock again? So I dont have to do

s3ClientWrapper = S3ClientWrapper(s3, BUCKET_NAME)