5

I have a sample method(which I need to write test case) as given below,

 fun setName(val auxName:String) {
        val configUrl = getConfig(auxName)
    }

I want to mock the getConfig method and return a specific string value. getConfig is a method in a Kotlin Object as below,

object Configuration{
    fun getConfig(auxName:String){
    ....
    }
    }

Below is the test that I tried

@Test
fun setTest()
{
val testname="test"
val testObject=Mockito.mock(Configuration::class.java)
doReturn("configTest").`when`(testObject).getConfig(Mockito.anyString())
setName(testname)
}

I am not getting any error but the method getConfig is not mocked. The actual implementation is executed. I tried using Powermockito also. Please help me with this

Amrutha Jaya Raj
  • 612
  • 2
  • 10
  • 32

1 Answers1

0

the problem is probably with singleton object, you can try this answer: https://stackoverflow.com/a/37978020/3703819

dey
  • 3,022
  • 1
  • 15
  • 25