From 0da162c1eda1920e65502fe1de2550a4f937c714 Mon Sep 17 00:00:00 2001 From: Chrostoq Date: Thu, 28 Jul 2022 21:15:54 +0200 Subject: [PATCH 1/2] Fix unmockkAll to work if constructor was mocked multiple times --- dsl/common/src/main/kotlin/io/mockk/API.kt | 9 +-------- .../kotlin/io/mockk/it/ConstructorMockTest.kt | 16 ++++++++-------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/dsl/common/src/main/kotlin/io/mockk/API.kt b/dsl/common/src/main/kotlin/io/mockk/API.kt index 0c802aceb..defebb02a 100644 --- a/dsl/common/src/main/kotlin/io/mockk/API.kt +++ b/dsl/common/src/main/kotlin/io/mockk/API.kt @@ -526,7 +526,7 @@ object MockKDsl { MockKCancellationRegistry .subRegistry(MockKCancellationRegistry.Type.CONSTRUCTOR) - .putIfNotThere(it, cancellation) + .cancelPut(it, cancellation) } } @@ -2302,13 +2302,6 @@ object MockKCancellationRegistry { map[key] = newCancellation } - fun putIfNotThere(key: Any, newCancellation: MockKCancellation) { - val map = mapTl.value - if (!map.containsKey(key)) { - map[key] = newCancellation - } - } - fun cancelAll() { val map = mapTl.value map.values.forEach { it() } diff --git a/mockk/common/src/test/kotlin/io/mockk/it/ConstructorMockTest.kt b/mockk/common/src/test/kotlin/io/mockk/it/ConstructorMockTest.kt index 22b0ba0d1..03de44a79 100644 --- a/mockk/common/src/test/kotlin/io/mockk/it/ConstructorMockTest.kt +++ b/mockk/common/src/test/kotlin/io/mockk/it/ConstructorMockTest.kt @@ -9,11 +9,6 @@ class ConstructorMockTest { val exampleProperty: Int = 1 } - object ExampleObject { - private val exampleClass = ExampleClass() - fun getExampleProperty(): Int = exampleClass.exampleProperty - } - data class MockCls(private val x: Int = 0) { constructor(x: String) : this(x.toInt()) @@ -31,16 +26,21 @@ class ConstructorMockTest { every { anyConstructed().exampleProperty } returns 0 - assertEquals(0, ExampleObject.getExampleProperty()) + assertEquals(0, ExampleClass().exampleProperty) } @Test - fun test2() { + fun unmockkAllTest() { + mockkConstructor(ExampleClass::class) mockkConstructor(ExampleClass::class) every { anyConstructed().exampleProperty } returns 0 - assertEquals(0, ExampleObject.getExampleProperty()) + assertEquals(0, ExampleClass().exampleProperty) + + unmockkAll() + + assertEquals(1, ExampleClass().exampleProperty) } @Test From 62da3ac3f5348b93528a24dce69b24006b56494c Mon Sep 17 00:00:00 2001 From: Chrostoq Date: Fri, 29 Jul 2022 12:30:06 +0200 Subject: [PATCH 2/2] Extent ConstructorMockTests for unmockkAll and constructedWith behaviour --- .../kotlin/io/mockk/it/ConstructorMockTest.kt | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/mockk/common/src/test/kotlin/io/mockk/it/ConstructorMockTest.kt b/mockk/common/src/test/kotlin/io/mockk/it/ConstructorMockTest.kt index 03de44a79..848d6f52b 100644 --- a/mockk/common/src/test/kotlin/io/mockk/it/ConstructorMockTest.kt +++ b/mockk/common/src/test/kotlin/io/mockk/it/ConstructorMockTest.kt @@ -3,6 +3,7 @@ package io.mockk.it import io.mockk.* import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFailsWith class ConstructorMockTest { class ExampleClass { @@ -40,7 +41,13 @@ class ConstructorMockTest { unmockkAll() + // mockkConstructor called multiple times, but unmockkAll should still be able to unmock it assertEquals(1, ExampleClass().exampleProperty) + + // Constructor not mocked -> MockkException + assertFailsWith { + every { anyConstructed().exampleProperty } returns 0 + } } @Test @@ -143,6 +150,48 @@ class ConstructorMockTest { } } + @Test + fun unmockkAllconstructedWith() { + mockkConstructor(MockCls::class) + mockkConstructor(MockCls::class) + + val checkConstructedWith = { a: Int, b: Int, c: Int -> + every { + constructedWith(OfTypeMatcher(String::class)).op(any(), any()) + } returns a + every { + constructedWith(EqMatcher(6)).op(any(), any()) + } returns b + every { + constructedWith(OfTypeMatcher(Int::class)).op(any(), any()) + } returns c + + assertEquals(a, MockCls("5").op(1, 2)) + assertEquals(b, MockCls(6).op(1, 2)) + assertEquals(c, MockCls(5).op(1, 2)) + } + + checkConstructedWith(23, 55, 35) + + // New mockkConstructor -> we can still mock as expected + mockkConstructor(MockCls::class) + checkConstructedWith(44, 101, 42) + + // mockkConstructor was called multiple times, but we can still unmock it via unmockkAll + unmockkAll() + + assertEquals(8, MockCls("5").op(1, 2)) + assertEquals(8, MockCls(5).op(1, 2)) + assertEquals(9, MockCls(6).op(1, 2)) + + // Constructor not mocked anymore -> MockkException expected + assertFailsWith { + every { + constructedWith(OfTypeMatcher(String::class)).op(any(), any()) + } returns 23 + } + } + @Test fun returnObject() { mockkConstructor(MockCls::class)