Skip to content

Commit fe18cc6

Browse files
committed
Include test to show how to mock a spy invocation with any()
1 parent f5bc442 commit fe18cc6

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

tests/src/test/kotlin/test/SpyTest.kt

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package test/*
1+
package test
2+
3+
/*
24
* The MIT License
35
*
46
* Copyright (c) 2016 Niek Haarman
@@ -93,7 +95,7 @@ class SpyTest : TestBase() {
9395
@Test
9496
fun doReturnWithDefaultInstanceSpyStubbing() {
9597
val timeVal = 12L
96-
98+
9799
val dateSpy = spy<Date> {
98100
on { time } doReturn timeVal
99101
}
@@ -104,16 +106,36 @@ class SpyTest : TestBase() {
104106
@Test
105107
fun doReturnWithSpyStubbing() {
106108
val timeVal = 15L
107-
109+
108110
val dateSpy = spy(Date(0)) {
109111
on { time } doReturn timeVal
110112
}
111113

112114
expect(dateSpy.time).toBe(timeVal)
113115
}
114116

115-
private interface MyInterface
116-
private open class MyClass : MyInterface
117+
@Test
118+
fun passAnyStringToSpy() {
119+
/* Given */
120+
val my = spy(MyClass())
121+
122+
/* When */
123+
doReturn("mocked").whenever(my).foo(any())
124+
125+
/* Then */
126+
expect(my.foo("hello")).toBe("mocked")
127+
}
128+
129+
private interface MyInterface {
130+
131+
fun foo(value: String): String
132+
}
133+
134+
private open class MyClass : MyInterface {
135+
136+
override fun foo(value: String): String = value
137+
}
138+
117139
private class ClosedClass
118140
}
119141

0 commit comments

Comments
 (0)