'How to mock function without class in Kotlin?

fun add() {
    return 4+1;
}


class Calculator {
    fun MathUtils() {
        // do something
        // calls add() function
        val x: Int = add()

        // return something
        return x + 22
    }
}


class CalculatorTest {
    var c = Calculator()
    
    @Test
    fun MathUtilsSuccess() {
    Assertions.assertThat(
            c.MathUtils()
        ).isEqualTo(24)
    }
}

I am new to unit Testing, I want to know is there any way I can call MathUtils() function (inside Calculator class)

in MathUtilsSuccess() (inside CalculatorTest class) and i have to mock add() function which is not inside any class such that add() always returns 2, so that in success scenario my Test pass.

All the classes are in separate file and fun add() is also in separate file.

P.S : I have broken down my doubt into this simple example, This is not the actual problem i am working on.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source