我有一个示例方法(我需要编写测试用例),如下所示,
fun setName(val auxName:String) {
val configUrl = getConfig(auxName)
}
我想模拟getConfig方法并返回一个特定的字符串值。 getConfig是Kotlin对象中的一个方法,如下所示,
object Configuration{
fun getConfig(auxName:String){
....
}
}
以下是我尝试过的测试
@Test
fun setTest()
{
val testname="test"
val testObject=Mockito.mock(Configuration::class.java)
doReturn("configTest").`when`(testObject).getConfig(Mockito.anyString())
setName(testname)
}
我没有收到任何错误,但方法getConfig没有被嘲笑。执行实际的实现。我也尝试过使用Powermockito。请在这件事上给予我帮助
问题可能是单身对象,你可以尝试这个答案:https://stackoverflow.com/a/37978020/3703819