mardi 2 février 2021

Mokito String cannot be returned by open() | open() should return InputStream

I am trying to test the method written below using Mokito but not able to do so.

fun getDataFromAsset(context: Context): String {
        return context.assets.open(ASSET_FILE_NAME).bufferedReader().use {
            it.readText()
        }
    }

Test for above method

@Test
        fun test_data_from_asset() {
            // Given
            whenever(context.assets).thenReturn(assetManager)
            whenever(context.assets.open(anyString()).thenReturn(
                IOUtils.toInputStream(anyString(), "UTF-8")
            )
            whenever(mokedSource.getDataFromAsset(context)).thenReturn(
                anyString())
    
            val flist = mokedSource.getDataListFromAsset(context)
            // Then
            assertEquals(flist, listOf())
        }

Error getting

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
String cannot be returned by open()
open() should return InputStream
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

Please help. Thanks in advance !

Aucun commentaire:

Enregistrer un commentaire