vendredi 15 janvier 2021

Mockk unmockk() method is not destroying mocks

I am running the following test code, but in some weird way I am not understanding it is not clearing the mocks in between the two tests.

I am getting environmentAsRequired as a return of repo.getClusterEnvironment(environmentName) on the second test instead of it throwing NoResultException() which causes the second test to fail because the exception I am asserting does not get thrown.

I have already verified and the method annotated with @BeforeEach is being called in between tests.

Anyone has any idea?

class ConfigUtilsTest {

@BeforeEach
fun start() {
    MockKAnnotations.init(this)
    val testModule = module() {
        single { mockk<Repository>() }
        single { mockk<AdminClient>() }
    }

    startKoin { modules(testModule) }
}

@AfterEach
fun stop() {
    stopKoin()
    unmockkAll()

}

@Test
fun `fetchOrCreateCluster creates the cluster if it is not on db but all required attributes are`() {

    val environmentName = "environmentForCluster"
    val clusterName = "NameForCluster"
    val clusterURL = "UrlForCluster"

    val environmentAsRequired = ClusterEnvironment(0, environmentName)
    val expectedResult = Cluster(
        clusterURL,
        clusterName,
        environmentAsRequired,
    )

    val repo = get(Repository::class.java)
    every { repo.getCluster(clusterURL) } throws NoResultException()
    every { repo.getClusterEnvironment(environmentName) } returns environmentAsRequired

    fetchOrCreateCluster(
        clusterName,
        environmentName,
        clusterURL
    ) shouldBe expectedResult
}

@Test
fun `fetchOrCreateCluster throws InvalidConfigException creating cluster if required attribute (environment) is not on DB`() {

    val clusterName = "NameForCluster"
    val clusterURL = "UrlForCluster"

    val repo = get(Repository::class.java)
    every { repo.getCluster(clusterURL) } throws NoResultException()
    every { repo.getClusterEnvironment(environmentName) } throws NoResultException()

    shouldThrow<InvalidConfigException> {
        fetchOrCreateCluster(
            clusterName,
            environmentName,
            clusterURL
        )
    }
}

}

Aucun commentaire:

Enregistrer un commentaire