File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
kotlin-testing/src/test/kotlin/com/baeldung/usingspy Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments