Skip to content

Commit fddfe9c

Browse files
spies (#815)
* commit * update * Update kotlin-testing/src/test/kotlin/com/baeldung/usingspy/CalculatorTests.kt Co-authored-by: Brandon Ward <[email protected]> * Update kotlin-testing/src/test/kotlin/com/baeldung/usingspy/CalculatorTests.kt Co-authored-by: Brandon Ward <[email protected]> * renaming the file * commit --------- Co-authored-by: Brandon Ward <[email protected]>
1 parent 4dc33ad commit fddfe9c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.usingspy
2+
3+
import io.mockk.clearMocks
4+
import io.mockk.every
5+
import io.mockk.spyk
6+
import io.mockk.verify
7+
import junit.framework.Assert.assertEquals
8+
import org.junit.jupiter.api.Test
9+
10+
class Calculator {
11+
fun add(a: Int, b: Int): Int {
12+
return a + b
13+
}
14+
fun findAverage(a: Int, b: Int): Int {
15+
val total = add(a, b)
16+
return total / 2;
17+
}
18+
}
19+
20+
class SpiesUnitTest {
21+
@Test
22+
fun testSpy() {
23+
val spy = spyk<Calculator>()
24+
val result = spy.findAverage(5, 5)
25+
assertEquals(5, result)
26+
}
27+
28+
@Test
29+
fun testPartialMocking() {
30+
val spy = spyk<Calculator>()
31+
every { spy.add(any(), any()) } returns 2
32+
val result = spy.findAverage(5, 5)
33+
verify { spy.add(5, 5) }
34+
assertEquals(1, result)
35+
clearMocks(spy)
36+
}
37+
}

0 commit comments

Comments
 (0)