In Scala, I noticed that when using when and thenReturn in Mockito, thenReturn runs the actual function before "replacing" the return value from the function. for example -
class clsToTest() {
def functionToTest: String = {
if(condition-that-evaluated-to-true-sometimes) {
throw new RuntimeException("some error")
}
"function-completed"
}
}
testclsToTest() {
test("test functionToTest") {
val spiedclsToTest = spy(new clsToTest())
when(spiedScalaSharedLibConfigManager.functionToTest).thenReturn("value to replace")
//spiedScalaSharedLibConfigManager.otherFunctionThatUsesFunctionToTest()
}
}
but when the line
when(spiedScalaSharedLibConfigManager.functionToTest).thenReturn("value to replace")
is executed, because sometimes condition-that-evaluated-to-true-sometimes
evaluated to True, the test fails without real reason.
I'm looking for a way to replace the value of the returned function without actually running the function.
I have my own reason to use spy instead of mocking.