diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue103Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue103Test.kt deleted file mode 100644 index 90967ef16..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue103Test.kt +++ /dev/null @@ -1,30 +0,0 @@ -package io.mockk.gh - -import io.mockk.every -import io.mockk.spyk -import io.mockk.verify -import kotlin.test.Test - -class Issue103Test { - class MyClass { - - fun myPublicMethod() { - myPrivateMethod() - } - - private fun myPrivateMethod() { - } - } - - @Test - fun getAdapter() { - val myClass = spyk(MyClass(), recordPrivateCalls = true) - every { myClass invokeNoArgs "myPrivateMethod" } returns Unit - - myClass.myPublicMethod() - - verify { - myClass invokeNoArgs "myPrivateMethod" - } - } -} \ No newline at end of file diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue109Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue109Test.kt deleted file mode 100644 index e6376944b..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue109Test.kt +++ /dev/null @@ -1,31 +0,0 @@ -package io.mockk.gh - -import io.mockk.* -import kotlin.test.Test - -class Issue109Test { - class Bar { - fun baz(foo: String) { - println(foo) - } - } - - class Foo { - override fun toString(): String { - return "foo" - } - } - - @Test - fun test() { - val foo = mockk() - val bar = mockk() - - every { bar.baz("$foo") } just runs - - bar.baz("$foo") - - verify(exactly = 1) { bar.baz("$foo") } - - } -} \ No newline at end of file diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue158Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue158Test.kt deleted file mode 100644 index 798bb1d1a..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue158Test.kt +++ /dev/null @@ -1,22 +0,0 @@ -package io.mockk.gh - -import io.mockk.every -import io.mockk.mockk -import kotlin.test.Test - -class Issue158Test { - class TestClass { - fun alwaysThrows() : Nothing { - throw RuntimeException("this can be any exception") - } - } - - private val targetClass = mockk() - - @Test - fun testNothingIsNotThrowingNPE() { - every { targetClass.alwaysThrows() } answers { - throw IllegalArgumentException("this is a test") - } - } -} diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue343Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue343Test.kt deleted file mode 100644 index b5b891081..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue343Test.kt +++ /dev/null @@ -1,25 +0,0 @@ -package io.mockk.gh - -import io.mockk.every -import io.mockk.mockk -import kotlin.test.Test -import kotlin.test.assertEquals - -class Issue343Test { - class MockCls { - fun op() = 0 - } - - val mock = mockk() - - @Test - fun andThenAnswer() { - // Setup - val firstAnswers = 1 - val secondAnswer = 2 - every { mock.op() } answers { firstAnswers } andThenAnswer { secondAnswer } - // Execute and Assert - assertEquals(firstAnswers, mock.op()) - assertEquals(secondAnswer, mock.op()) - } -} diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue346Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue346Test.kt deleted file mode 100644 index fb838c41f..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue346Test.kt +++ /dev/null @@ -1,21 +0,0 @@ -package io.mockk.gh - -import io.mockk.* -import kotlin.test.Test - -class Issue346Test { - class Cls { - private fun privateCall() = Unit - - fun pubCall() = privateCall() - } - - @Test - fun test() { - val mock = spyk(recordPrivateCalls = true) - - justRun { mock invokeNoArgs "privateCall" } - - mock.pubCall() - } -} diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue352Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue352Test.kt deleted file mode 100644 index 99f85d8ab..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue352Test.kt +++ /dev/null @@ -1,86 +0,0 @@ -package io.mockk.gh - -import io.mockk.MockKException -import io.mockk.every -import io.mockk.mockk -import io.mockk.slot -import io.mockk.verify -import io.mockk.verifyOrder -import kotlin.test.BeforeTest -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith - -class Issue352Test { - - open class MockedSubject { - open fun doSomething(id: String?, data: Any?): String { - throw IllegalStateException("Not mocked :(") - } - } - - private val mock = mockk() - - @BeforeTest - fun setup() { - every { mock.doSomething("1", "data1") } returns "result1" - every { mock.doSomething("2", "data2") } returns "result2" - } - - @Test - fun itThrowsAMockkExceptionWhenVerifyingTheSameFunctionTwiceWithSlots() { - mock.doSomething("1", "data1") - mock.doSomething("2", "data2") - - val dataSlotId1 = slot() - val dataSlotId2 = slot() - - assertFailsWith { - verify { - mock.doSomething("1", capture(dataSlotId1)) - mock.doSomething("2", capture(dataSlotId2)) - } - } - } - - @Test - fun itDoesNotThrowAMockkExceptionWhenThereAreMultipleTestsVerifyingWithSlots() { - mock.doSomething("1", "data1") - - val slot = slot() - verify { - mock.doSomething("1", capture(slot)) - } - - assertEquals("data1", slot.captured) - } - - @Test - fun anotherTestToTestTheCoexistenceOfTestsWithSlots() { - mock.doSomething("1", "data1") - - val slot = slot() - verify { - mock.doSomething("1", capture(slot)) - } - - assertEquals("data1", slot.captured) - } - - @Test - fun itAllowsMultipleCapturingsOfTheSameFunctionUsingAMutableList() { - mock.doSomething("1", "data1") - mock.doSomething("2", "data2") - - val slotList = mutableListOf() - - verify { - mock.doSomething("1", capture(slotList)) - mock.doSomething("2", capture(slotList)) - } - - assertEquals("data1", slotList[0]) - assertEquals("data2", slotList[1]) - } - -} diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue353Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue353Test.kt deleted file mode 100644 index 872e4cd66..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue353Test.kt +++ /dev/null @@ -1,32 +0,0 @@ -package io.mockk.gh - -import io.mockk.Runs -import io.mockk.every -import io.mockk.just -import io.mockk.mockk -import kotlin.test.Test -import kotlin.test.assertEquals - -class Issue353Test { - - @Test - fun testNullableCapture() { - class Mock { - fun call(unused: String?) { - println(unused) - } - } - - val list = mutableListOf() - val test: Mock = mockk { - every { call(captureNullable(list)) } just Runs - } - - val args = listOf("One", "Two", "Three", null) - for (arg in args) { - test.call(arg) - } - - assertEquals(args, list) - } -} diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue389Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue389Test.kt deleted file mode 100644 index 7f458e084..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue389Test.kt +++ /dev/null @@ -1,42 +0,0 @@ -package io.mockk.gh - -import io.mockk.mockk -import io.mockk.verifyAll -import kotlin.test.Ignore -import kotlin.test.Test -import kotlin.test.assertEquals - -class Tweet(val id: Int, val text: String) - -interface TweetRepository { - - fun persist(tweet: Tweet) - -} - -class Issue389Test { - - @Test - @Ignore - // Temporarily ignored because it suddenly started failing only on Github actions - internal fun verifyMultiplePersists() { - val repositoryMock = mockk(relaxed = true) - - repositoryMock.persist(Tweet(1, "first tweet")) - repositoryMock.persist(Tweet(2, "second tweet")) - - - verifyAll { - repositoryMock.persist( - withArg { - assertEquals(it.id, 1) - assertEquals(it.text, "first tweet") - }) - repositoryMock.persist( - withArg { - assertEquals(it.id,2) - assertEquals(it.text, "second tweet") - }) - } - } -} diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue507Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue507Test.kt deleted file mode 100644 index f5f4e99f2..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue507Test.kt +++ /dev/null @@ -1,44 +0,0 @@ -package io.mockk.gh - -import io.mockk.mockk -import io.mockk.verifyOrder -import kotlin.test.Test - -interface Tracker { - fun track(song: String, action: String, param: String, moreParam: Map) -} - -class Player(private val tracker: Tracker) { - fun goCrazy() { - tracker.track("song 2", "play", "param", mapOf(Pair("key", "value"))) - tracker.track("song 2", "pause", "param", mapOf(Pair("key", "value"))) - tracker.track("song 2", "play", "param", mapOf(Pair("key", "value"))) - tracker.track("song 2", "pause", "param", mapOf(Pair("key", "value"))) - tracker.track("song 2", "play", "param", mapOf(Pair("key", "value"))) - } -} - - -class Issue507Test { - - /** - * A regression occurred in version 1.10.2 causing verify order to use - * eq() instead of any() matcher. - * This test exist to avoid this kind of regression in the future. - */ - @Test - fun checkAgainstVerifyOrder() { - val tracker = mockk(relaxUnitFun = true) - val player = Player(tracker) - - player.goCrazy() - - verifyOrder { - tracker.track(any(), "play", "param", any()) - tracker.track(any(), "pause", "param", any()) - tracker.track(any(), "play", "param", any()) - tracker.track(any(), "pause", "param", any()) - tracker.track(any(), "play", "param", any()) - } - } -} diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue510Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue510Test.kt deleted file mode 100644 index cb6950122..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue510Test.kt +++ /dev/null @@ -1,56 +0,0 @@ -package io.mockk.gh - -import io.mockk.clearAllMocks -import io.mockk.every -import io.mockk.mockk -import kotlin.test.BeforeTest -import kotlin.test.Test - -data class Product(val name: String, val price: Int) -data class Order(val name: String) - -class ShopService { - - fun buyProducts(products: List) { - println("You bought $products...") - } - - fun addProductAndOrders(products: List, orders: List) { - println("Add $products and $orders...") - } -} - -class TestMockk { - - private val shopService = mockk() - - @BeforeTest - internal fun setUp() { - clearAllMocks() - } - - @Test - internal fun shouldMatchListOfArguments() { // Passes - - every { - shopService.buyProducts(any()) - } returns Unit - val products = listOf(Product("raspberry", 2), Product("banana", 212)) - - shopService.buyProducts(products) - - } - - @Test - internal fun shouldMatchWithTwoArgumentsOfTypeList() { // Throws MockkException - - every { - shopService.addProductAndOrders(products = any(), orders = any()) - } returns Unit - - val products = listOf(Product("raspberry", 2), Product("banana", 1)) - val orders = listOf(Order("raspber"), Order("banana")) - - shopService.addProductAndOrders(products, orders) // Throws MockkException - } -} diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue614Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue614Test.kt deleted file mode 100644 index 48c93fed6..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue614Test.kt +++ /dev/null @@ -1,20 +0,0 @@ -package io.mockk.gh - -import io.mockk.mockk -import io.mockk.verifyOrder -import kotlin.test.Test -import kotlin.test.assertFailsWith - -class Issue614Test { - - @Test - fun verifyOrderThrowAssertionErrorIfNoCallHasBeenMade() { - val mock: Something = mockk(relaxed = true, relaxUnitFun = true) - - assertFailsWith { verifyOrder { mock.doSomething() } } - } - - open class Something { - fun doSomething() {} - } -} diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue88Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue88Test.kt deleted file mode 100644 index 19a050046..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue88Test.kt +++ /dev/null @@ -1,51 +0,0 @@ -package io.mockk.gh - -import io.mockk.* -import kotlin.test.Test -import kotlin.test.assertFailsWith - -@Suppress("UNUSED_PARAMETER") -class Issue88Test { - open class B {} - class C : B() {} - class A { fun go(x: B) {} } - - @Test - fun test1() { - val mock = mockk() - - every { mock.go(ofType()) } just Runs - - assertFailsWith(MockKException::class) { - mock.go(B()) - } - } - - @Test - fun test2() { - val mock = mockk() - - every { mock.go(ofType()) } just Runs - - mock.go(C()) - } - - - @Test - fun test3() { - val mock = mockk() - - every { mock.go(ofType()) } just Runs - - mock.go(B()) - } - - @Test - fun test4() { - val mock = mockk() - - every { mock.go(ofType()) } just Runs - - mock.go(C()) - } -} \ No newline at end of file diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue95Test.kt b/mockk/common/src/test/kotlin/io/mockk/gh/Issue95Test.kt deleted file mode 100644 index 545f4e20b..000000000 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue95Test.kt +++ /dev/null @@ -1,41 +0,0 @@ -package io.mockk.gh - -import io.mockk.every -import io.mockk.spyk -import kotlin.test.Test - -@Suppress("UNUSED_PARAMETER") -class Issue95Test { - class Foo { - - fun getInterface(list: List) = listOf(true) - - fun bar(t: String, list: List = getInterface(listOf(false))): String { - return "FOO" - } - - fun bar2(list: List = getInterface(listOf(false))): String { - return "FOO" - } - } - - @Test - fun testMock() { - val mocking = spyk(Foo()) - - every { - mocking.bar(any()) - } returns "FOO MOCKED" - - println(mocking.bar("hello")) - } - - @Test - fun test2() { - val mocking = spyk(Foo()) - - every { mocking.bar2() } returns "FOO MOCKED" - - println(mocking.bar2()) - } -} \ No newline at end of file diff --git a/mockk/common/src/test/kotlin/io/mockk/it/CapturingTest.kt b/mockk/common/src/test/kotlin/io/mockk/it/CapturingTest.kt index 5a6b46ebf..dd9f310cb 100644 --- a/mockk/common/src/test/kotlin/io/mockk/it/CapturingTest.kt +++ b/mockk/common/src/test/kotlin/io/mockk/it/CapturingTest.kt @@ -1,21 +1,19 @@ package io.mockk.it import io.mockk.* +import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFailsWith class CapturingTest { - class Cls { - var value = 0 - } + private val mock = mockk() - class MockCls { - fun op(a: Int, b: Int, c: Cls) = a + b + c.value - } - - class CoMockCls { - suspend fun op(a: Int, b: Int, c: Cls) = a + b + c.value + @BeforeTest + fun setup() { + every { mock.doSomething("1", "data1") } returns "result1" + every { mock.doSomething("2", "data2") } returns "result2" } @Test @@ -73,4 +71,114 @@ class CapturingTest { coVerify { mock.op(1, 2, any()) } } -} \ No newline at end of file + + /** + * See issue #352. + */ + @Test + fun itThrowsAMockkExceptionWhenVerifyingTheSameFunctionTwiceWithSlots() { + mock.doSomething("1", "data1") + mock.doSomething("2", "data2") + + val dataSlotId1 = slot() + val dataSlotId2 = slot() + + assertFailsWith { + verify { + mock.doSomething("1", capture(dataSlotId1)) + mock.doSomething("2", capture(dataSlotId2)) + } + } + } + + /** + * See issue #352. + */ + @Test + fun itDoesNotThrowAMockkExceptionWhenThereAreMultipleTestsVerifyingWithSlots() { + mock.doSomething("1", "data1") + + val slot = slot() + verify { + mock.doSomething("1", capture(slot)) + } + + assertEquals("data1", slot.captured) + } + + /** + * See issue #352. + */ + @Test + fun anotherTestToTestTheCoexistenceOfTestsWithSlots() { + mock.doSomething("1", "data1") + + val slot = slot() + verify { + mock.doSomething("1", capture(slot)) + } + + assertEquals("data1", slot.captured) + } + + /** + * See issue #352. + */ + @Test + fun itAllowsMultipleCapturingsOfTheSameFunctionUsingAMutableList() { + mock.doSomething("1", "data1") + mock.doSomething("2", "data2") + + val slotList = mutableListOf() + + verify { + mock.doSomething("1", capture(slotList)) + mock.doSomething("2", capture(slotList)) + } + + assertEquals("data1", slotList[0]) + assertEquals("data2", slotList[1]) + } + + /** + * See issue #353. + */ + @Test + fun testNullableCapture() { + val list = mutableListOf() + val test: MockNullableCls = mockk { + every { call(captureNullable(list)) } just Runs + } + + val args = listOf("One", "Two", "Three", null) + for (arg in args) { + test.call(arg) + } + + assertEquals(args, list) + } + + class MockNullableCls { + fun call(unused: String?) { + println(unused) + } + } + + class Cls { + var value = 0 + } + + class MockCls { + fun op(a: Int, b: Int, c: Cls) = a + b + c.value + } + + class CoMockCls { + suspend fun op(a: Int, b: Int, c: Cls) = a + b + c.value + } + + open class MockedSubject { + open fun doSomething(id: String?, data: Any?): String { + throw IllegalStateException("Not mocked :(") + } + } +} diff --git a/mockk/common/src/test/kotlin/io/mockk/it/MatcherTest.kt b/mockk/common/src/test/kotlin/io/mockk/it/MatcherTest.kt index daa913418..15b42b8e5 100644 --- a/mockk/common/src/test/kotlin/io/mockk/it/MatcherTest.kt +++ b/mockk/common/src/test/kotlin/io/mockk/it/MatcherTest.kt @@ -1,31 +1,27 @@ package io.mockk.it -import io.mockk.* +import io.mockk.MockKAnnotations +import io.mockk.MockKException +import io.mockk.Runs +import io.mockk.every import io.mockk.impl.annotations.MockK +import io.mockk.just +import io.mockk.mockk +import io.mockk.slot +import io.mockk.verify import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith class MatcherTest { - interface Wrapper - data class IntWrapper(val data: Int) : Wrapper - - class MockCls { - fun op(a: Int, b: Int): Int = a + b - - fun op(a: Wrapper?, b: Wrapper?): Int { - return if (a is IntWrapper && b is IntWrapper) { - a.data + b.data - } else { - 0 - } - } - } @MockK lateinit var mock: MockCls + @MockK + private lateinit var shopService: ShopService + @BeforeTest fun init() { MockKAnnotations.init(this) @@ -46,19 +42,19 @@ class MatcherTest { mock.op(IntWrapper(3), b) } } - + @Test fun nEqNRefEq() { val a = IntWrapper(3) val b = IntWrapper(4) - + every { mock.op(neq(a), nrefEq(b)) } returns 1 - + assertEquals(1, mock.op(b, a), "Answer should be one, as b != a and a != b, so both neq and nrefEq.") assertFailsWith("Should fail because a is eq to a, so neq fails") { mock.op(a, IntWrapper(4)) } assertFailsWith("Should fail because b is referencial equal tob, so nrefEq fails") { mock.op(b, b) } assertEquals(1, mock.op(b, IntWrapper(3))) - + verify { mock.op(b, IntWrapper(3)) } @@ -311,4 +307,86 @@ class MatcherTest { mock.op(IntWrapper(7), IntWrapper(8)) } } -} \ No newline at end of file + + /** + * See issue 88 + */ + @Test + fun ofTypeWithGenerics() { + val mock = mockk() + every { mock.go(ofType()) } just Runs + assertFailsWith(MockKException::class) { mock.go(B()) } + + every { mock.go(ofType()) } just Runs + mock.go(C()) + + every { mock.go(ofType()) } just Runs + mock.go(B()) + + every { mock.go(ofType()) } just Runs + mock.go(C()) + } + + /** + * See issue #510 + */ + @Test + fun anyWithLists() { + every { + shopService.buyProducts(any()) + } returns Unit + val products = listOf(Product("raspberry", 2), Product("banana", 212)) + + shopService.buyProducts(products) + } + + /** + * See issue #510 + */ + @Test + fun anyWithTwoListArgument() { + every { + shopService.addProductAndOrders(products = any(), orders = any()) + } returns Unit + + val products = listOf(Product("raspberry", 2), Product("banana", 1)) + val orders = listOf(Order("raspber"), Order("banana")) + + shopService.addProductAndOrders(products, orders) // Throws MockkException + } + + interface Wrapper + data class IntWrapper(val data: Int) : Wrapper + + class MockCls { + fun op(a: Int, b: Int): Int = a + b + + fun op(a: Wrapper?, b: Wrapper?): Int { + return if (a is IntWrapper && b is IntWrapper) { + a.data + b.data + } else { + 0 + } + } + } + + open class B {} + class C : B() {} + class A { + fun go(x: B) {} + } + + data class Product(val name: String, val price: Int) + data class Order(val name: String) + + class ShopService { + + fun buyProducts(products: List) { + println("You bought $products...") + } + + fun addProductAndOrders(products: List, orders: List) { + println("Add $products and $orders...") + } + } +} diff --git a/mockk/common/src/test/kotlin/io/mockk/it/MockkClassTest.kt b/mockk/common/src/test/kotlin/io/mockk/it/MockkClassTest.kt index 263b1faa4..9de8cf6dc 100644 --- a/mockk/common/src/test/kotlin/io/mockk/it/MockkClassTest.kt +++ b/mockk/common/src/test/kotlin/io/mockk/it/MockkClassTest.kt @@ -1,6 +1,7 @@ package io.mockk.it import io.mockk.every +import io.mockk.mockk import io.mockk.mockkClass import io.mockk.verify import kotlin.test.Test @@ -8,10 +9,6 @@ import kotlin.test.assertEquals class MockkClassTest { - class MockCls { - fun op(a: Int, b: Int) = a + b - } - val mock = mockkClass(MockCls::class) /** @@ -25,4 +22,26 @@ class MockkClassTest { verify { mock.op(3, 4) } } + + private val targetClass = mockk() + + /** + * See issue #158 + */ + @Test + fun testNothingIsNotThrowingNPE() { + every { targetClass.alwaysThrows() } answers { + throw IllegalArgumentException("this is a test") + } + } + + class TestClass { + fun alwaysThrows() : Nothing { + throw RuntimeException("this can be any exception") + } + } + + class MockCls { + fun op(a: Int, b: Int) = a + b + } } diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue312Test.kt b/mockk/common/src/test/kotlin/io/mockk/it/ParametersWhitDefaultTest.kt similarity index 74% rename from mockk/common/src/test/kotlin/io/mockk/gh/Issue312Test.kt rename to mockk/common/src/test/kotlin/io/mockk/it/ParametersWhitDefaultTest.kt index 07437e5cd..6c7fe82cf 100644 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue312Test.kt +++ b/mockk/common/src/test/kotlin/io/mockk/it/ParametersWhitDefaultTest.kt @@ -1,19 +1,17 @@ -package io.mockk.gh +package io.mockk.it import io.mockk.every import io.mockk.mockk import kotlin.test.Test import kotlin.test.assertEquals -interface DefaultParam { - fun foo(param: String = "default"): String -} - -class Issue312Test { +class ParametersWhitDefaultTest { private val target = mockk() /** + * See issue #312. + * * Mocking a function with default parameter should match without specifying its * parameters. */ @@ -27,4 +25,8 @@ class Issue312Test { private companion object { const val STUB_STRING = "A string" } + + interface DefaultParam { + fun foo(param: String = "default"): String + } } diff --git a/mockk/common/src/test/kotlin/io/mockk/it/PrivateFunctionsTest.kt b/mockk/common/src/test/kotlin/io/mockk/it/PrivateFunctionsTest.kt index c0bbcf44a..cb08bdce4 100644 --- a/mockk/common/src/test/kotlin/io/mockk/it/PrivateFunctionsTest.kt +++ b/mockk/common/src/test/kotlin/io/mockk/it/PrivateFunctionsTest.kt @@ -1,6 +1,13 @@ package io.mockk.it -import io.mockk.* +import io.mockk.Runs +import io.mockk.every +import io.mockk.just +import io.mockk.justRun +import io.mockk.mockkObject +import io.mockk.spyk +import io.mockk.verify +import io.mockk.verifySequence import kotlin.test.Test import kotlin.test.assertEquals @@ -75,6 +82,43 @@ class PrivateFunctionsTest { verify { mock["x"](any(), any(), any()) } } + /** + * See issue #103 + */ + @Test + fun mockPrivateMethodThatReturnsNothing() { + val myClass = spyk(PrivateNoReturnCls(), recordPrivateCalls = true) + every { myClass invokeNoArgs "myPrivateMethod" } returns Unit + + myClass.myPublicMethod() + + verify { + myClass invokeNoArgs "myPrivateMethod" + } + } + + /** + * See issue #346 + */ + @Test + fun justRunsWithPrivateMethod() { + val mock = spyk(recordPrivateCalls = true) + + justRun { mock invokeNoArgs "myPrivateMethod" } + + mock.myPublicMethod() + } + + class PrivateNoReturnCls { + + fun myPublicMethod() { + myPrivateMethod() + } + + private fun myPrivateMethod() { + } + } + class Abc { fun y() = x() diff --git a/mockk/common/src/test/kotlin/io/mockk/gh/Issue221Test.kt b/mockk/common/src/test/kotlin/io/mockk/it/SetsTest.kt similarity index 86% rename from mockk/common/src/test/kotlin/io/mockk/gh/Issue221Test.kt rename to mockk/common/src/test/kotlin/io/mockk/it/SetsTest.kt index 7dbc6ee65..03c496bec 100644 --- a/mockk/common/src/test/kotlin/io/mockk/gh/Issue221Test.kt +++ b/mockk/common/src/test/kotlin/io/mockk/it/SetsTest.kt @@ -1,4 +1,4 @@ -package io.mockk.gh +package io.mockk.it import io.mockk.every import io.mockk.isMockKMock @@ -7,20 +7,14 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue -class Issue221Test { - interface Foo { - fun getTasks(): Set - - fun getTask(): Task - } - - interface Task { - fun getSubTask(): Task - - fun doIt(): Int - } - +/** + * [Set]s related tests. + */ +class SetsTest { + /** + * See issue #221 + */ @Test fun returnsSetOfMocks() { val foo = mockk() @@ -36,4 +30,15 @@ class Issue221Test { assertTrue(isMockKMock(task2)) } -} \ No newline at end of file + interface Foo { + fun getTasks(): Set + + fun getTask(): Task + } + + interface Task { + fun getSubTask(): Task + + fun doIt(): Int + } +} diff --git a/mockk/common/src/test/kotlin/io/mockk/it/SpyTestCls.kt b/mockk/common/src/test/kotlin/io/mockk/it/SpyTestCls.kt index 5dfe8b5dd..3bc91b70c 100644 --- a/mockk/common/src/test/kotlin/io/mockk/it/SpyTestCls.kt +++ b/mockk/common/src/test/kotlin/io/mockk/it/SpyTestCls.kt @@ -79,6 +79,40 @@ class SpyTest { assertTrue(executed[3]) } + /** + * See issue #95 + */ + @Test + fun chainedCalls() { + val mocking = spyk(ChainedCallsCls()) + + every { mocking.bar(any()) } returns "FOO MOCKED" + + println(mocking.bar("hello")) + } + + @Test + fun chainedCalls2() { + val mocking = spyk(ChainedCallsCls()) + + every { mocking.bar2() } returns "FOO MOCKED" + + println(mocking.bar2()) + } + + class ChainedCallsCls { + + fun getInterface(list: List) = listOf(true) + + fun bar(t: String, list: List = getInterface(listOf(false))): String { + return "FOO" + } + + fun bar2(list: List = getInterface(listOf(false))): String { + return "FOO" + } + } + open class BaseTestCls(val someReference: String, val executed: Array) { open fun doSomething() { executed[0] = true @@ -103,4 +137,4 @@ class SpyTest { return 5 + a } } -} \ No newline at end of file +} diff --git a/mockk/common/src/test/kotlin/io/mockk/it/VerifyAtLeastAtMostExactlyTest.kt b/mockk/common/src/test/kotlin/io/mockk/it/VerifyAtLeastAtMostExactlyTest.kt index 057bf1458..a7ebd348d 100644 --- a/mockk/common/src/test/kotlin/io/mockk/it/VerifyAtLeastAtMostExactlyTest.kt +++ b/mockk/common/src/test/kotlin/io/mockk/it/VerifyAtLeastAtMostExactlyTest.kt @@ -1,15 +1,16 @@ package io.mockk.it -import io.mockk.* +import io.mockk.Called +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import io.mockk.verifyOrder +import io.mockk.verifySequence import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith class VerifyAtLeastAtMostExactlyTest { - class MockCls { - fun op(a: Int) = a + 1 - fun op2(a: Int, b: Int) = a + b - } val mock = mockk() @@ -145,6 +146,39 @@ class VerifyAtLeastAtMostExactlyTest { } } + /** + * See issue #507 + * + * A regression occurred in version 1.10.2 causing verify order to use + * eq() instead of any() matcher. + * This test exist to avoid this kind of regression in the future. + */ + @Test + fun orderWithAny() { + val tracker = mockk(relaxUnitFun = true) + val player = Player(tracker) + + player.goCrazy() + + verifyOrder { + tracker.track(any(), "play", "param", any()) + tracker.track(any(), "pause", "param", any()) + tracker.track(any(), "play", "param", any()) + tracker.track(any(), "pause", "param", any()) + tracker.track(any(), "play", "param", any()) + } + } + + /** + * See issue #614 + */ + @Test + fun verifyOrderThrowAssertionErrorIfNoCallHasBeenMade() { + val mock: MockCls = mockk(relaxed = true, relaxUnitFun = true) + + assertFailsWith { verifyOrder { mock.op(any()) } } + } + @Test fun sequence() { doCalls() @@ -188,4 +222,22 @@ class VerifyAtLeastAtMostExactlyTest { assertEquals(3, mock.op2(2, 1)) } + class MockCls { + fun op(a: Int) = a + 1 + fun op2(a: Int, b: Int) = a + b + } + + interface Tracker { + fun track(song: String, action: String, param: String, moreParam: Map) + } + + class Player(private val tracker: Tracker) { + fun goCrazy() { + tracker.track("song 2", "play", "param", mapOf(Pair("key", "value"))) + tracker.track("song 2", "pause", "param", mapOf(Pair("key", "value"))) + tracker.track("song 2", "play", "param", mapOf(Pair("key", "value"))) + tracker.track("song 2", "pause", "param", mapOf(Pair("key", "value"))) + tracker.track("song 2", "play", "param", mapOf(Pair("key", "value"))) + } + } } diff --git a/mockk/common/src/test/kotlin/io/mockk/it/VerifyTest.kt b/mockk/common/src/test/kotlin/io/mockk/it/VerifyTest.kt index 7abefc72c..c546f4c00 100644 --- a/mockk/common/src/test/kotlin/io/mockk/it/VerifyTest.kt +++ b/mockk/common/src/test/kotlin/io/mockk/it/VerifyTest.kt @@ -1,13 +1,18 @@ package io.mockk.it -import io.mockk.* +import io.mockk.every +import io.mockk.just +import io.mockk.mockk +import io.mockk.runs +import io.mockk.verify +import io.mockk.verifyAll +import io.mockk.verifyOrder +import io.mockk.verifySequence +import kotlin.test.Ignore import kotlin.test.Test import kotlin.test.assertEquals class VerifyTest { - class MockCls { - fun op(a: Int) = a + 1 - } val mock = mockk() @@ -153,4 +158,71 @@ class VerifyTest { mock.op(7) } } -} \ No newline at end of file + + /** + * See issue #109 + */ + @Test + fun verifyWithToString() { + val foo = mockk() + val bar = mockk() + + every { bar.baz("$foo") } just runs + + bar.baz("$foo") + + verify(exactly = 1) { bar.baz("$foo") } + + } + + /** + * See issue #389. + */ + @Test + @Ignore + // Temporarily ignored because it suddenly started failing only on Github actions + internal fun verifyUsingVerifyAll() { + val repositoryMock = mockk(relaxed = true) + + repositoryMock.persist(Tweet(1, "first tweet")) + repositoryMock.persist(Tweet(2, "second tweet")) + + + verifyAll { + repositoryMock.persist( + withArg { + assertEquals(it.id, 1) + assertEquals(it.text, "first tweet") + }) + repositoryMock.persist( + withArg { + assertEquals(it.id, 2) + assertEquals(it.text, "second tweet") + }) + } + } + + class Bar { + fun baz(foo: String) { + println(foo) + } + } + + class Foo { + override fun toString(): String { + return "foo" + } + } + + class MockCls { + fun op(a: Int) = a + 1 + } + + class Tweet(val id: Int, val text: String) + + interface TweetRepository { + + fun persist(tweet: Tweet) + + } +}