dimanche 7 mars 2021

Mockk clear mocks between test execution

I don't understand exactly why those tests are working correctly (it's not real code but it's the same as my class):

class AClassTest {
    private val dep: Dep = mockk()

    private val testedClass = TestedClass(dep)

    @Test
    fun testMethod1() {
        testedClass.method1();
        verify(exactly = 1) { dep.method() }
    }

    @Test
    fun testMethod2() {
        testedClass.method2();
        verify(exactly = 1) { dep.method() }
    }
}

Why do tests pass without resetting mocks? Is Mockk resetting mocks automatically? I expected a @BeforeEach or an @AfterEach method calling clearMocks to clear mock state.

Maybe clearing is necessary only to clear behaviours, not inspected data? So in this case, why those tests are working correctly?

class AClassTest {
    private val dep: Dep = mockk()

    private val testedClass = TestedClass(dep)

    @Test
    fun testMethodOnIllegalArgumentException() {
        every { dep.method() } throws IllegalArgumentException()
        assertThrows<IllegalArgumentException> { testedClass.method() }
    }

    @Test
    fun testMethodOnIndexOutOfBoundsException() {
        every { dep.method() } throws IndexOutOfBoundsException()
        assertThrows<IndexOutOfBoundsException> { testedClass.method() }
    }
}

Aucun commentaire:

Enregistrer un commentaire