jeudi 5 novembre 2020

Test function with default non-constant arguments

I have a method which works with time and therefore I need to pass current time to it as an argument in order to make it testable:

fun dummy(now: Instant = Instant()) = true

However, I cannot mock it with Mockito, I think because in the code it is called without the default argument. classA.dummy() always returns false. I cannot use dummy(any()) because Mockito complains it is null. How to test this kind of functions?

@Test
fun test() {
    whenever(classA.dummy()).thenReturn(true)

    classUnderTest.execute()

    verify(classB).execute()
}

class ClassUnderTest {

    @Inject lateinit var classA: ClassA
    @Inject lateinit var classB: ClassB

    fun execute() {
       if (classA.dummy()) classB.execute()
    }

 }

Aucun commentaire:

Enregistrer un commentaire