1
+ package com.baeldung.mockk
2
+
3
+ import io.mockk.MockKException
4
+ import io.mockk.every
5
+ import io.mockk.mockk
6
+ import org.junit.jupiter.api.Test
7
+ import org.junit.jupiter.api.assertThrows
8
+ import kotlin.test.assertEquals
9
+ import kotlin.test.assertTrue
10
+
11
+ class MyClass {
12
+ fun joinBySpace (vararg strings : String ): String {
13
+ return strings.joinToString(separator = " " )
14
+ }
15
+ }
16
+
17
+ class MatchVarargsUnitTest {
18
+ private val mockkObj = mockk<MyClass >()
19
+
20
+ @Test
21
+ fun `when calling concatStringsBySpace() on the real object then get expected result` () {
22
+ val result = MyClass ().joinBySpace(" a" , " b" , " c" , " d" , " e" )
23
+ assertEquals(" a b c d e" , result)
24
+ }
25
+
26
+ @Test
27
+ fun `when using anyVararg() on the mocked object then get expected result` () {
28
+ every { mockkObj.joinBySpace(" a" , " b" , " c" , * anyVararg(), " x" , " y" , " z" ) } returns " Wow, Kotlin rocks!"
29
+
30
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " is" , " cool" , " x" , " y" , " z" )
31
+ assertEquals(" Wow, Kotlin rocks!" , result)
32
+
33
+ val result2 = mockkObj.joinBySpace(" a" , " b" , " c" , " x" , " y" , " z" )
34
+ assertEquals(" Wow, Kotlin rocks!" , result2)
35
+ }
36
+
37
+
38
+ @Test
39
+ fun `when using varargAll() on the mocked object then get expected result` () {
40
+ every { mockkObj.joinBySpace(" a" , " b" , " c" , * varargAll { it.startsWith(" d" ) }, " z" ) } returns " Wow, Kotlin rocks!"
41
+
42
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " d1" , " d2" , " d-whatever" , " z" )
43
+ assertEquals(" Wow, Kotlin rocks!" , result)
44
+
45
+ assertThrows<MockKException > {
46
+ mockkObj.joinBySpace(" a" , " b" , " c" , " d1" , " Baeldung" , " z" )
47
+ }.also { exception ->
48
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
49
+ }
50
+
51
+ val result2 = mockkObj.joinBySpace(" a" , " b" , " c" , " z" )
52
+ assertEquals(" Wow, Kotlin rocks!" , result2)
53
+ }
54
+
55
+ @Test
56
+ fun `when using varargAny() on the mocked object then get expected result` () {
57
+ every { mockkObj.joinBySpace(" a" , " b" , " c" , * varargAny { it.startsWith(" d" ) }, " z" ) } returns " Wow, Kotlin rocks!"
58
+
59
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " d1" , " d2" , " d-whatever" , " z" )
60
+ assertEquals(" Wow, Kotlin rocks!" , result)
61
+
62
+ val result2 = mockkObj.joinBySpace(" a" , " b" , " c" , " d1" , " Baeldung" , " z" )
63
+ assertEquals(" Wow, Kotlin rocks!" , result2)
64
+
65
+ assertThrows<MockKException > {
66
+ mockkObj.joinBySpace(" a" , " b" , " c" , " z" )
67
+ }.also { exception ->
68
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
69
+ }
70
+
71
+ }
72
+
73
+
74
+ @Test
75
+ fun `when using varargAll() with nArgs on the mocked object then get expected result` () {
76
+ every { mockkObj.joinBySpace(" a" , " b" , " c" , * varargAll { nArgs > 6 }, " z" ) } returns " Wow, Kotlin rocks!"
77
+
78
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " is" , " cool" , " z" )
79
+ assertEquals(" Wow, Kotlin rocks!" , result)
80
+
81
+ assertThrows<MockKException > {
82
+ mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " z" )
83
+ }.also { exception ->
84
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
85
+ }
86
+ }
87
+
88
+ @Test
89
+ fun `when using varargAny() with nArgs on the mocked object then get expected result` () {
90
+ every { mockkObj.joinBySpace(" a" , " b" , " c" , * varargAny { nArgs > 6 }, " z" ) } returns " Wow, Kotlin rocks!"
91
+
92
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " is" , " cool" , " z" )
93
+ assertEquals(" Wow, Kotlin rocks!" , result)
94
+
95
+ assertThrows<MockKException > {
96
+ mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " z" )
97
+ }.also { exception ->
98
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
99
+ }
100
+ }
101
+
102
+
103
+ @Test
104
+ fun `when using varargAll() with position on the mocked object then get expected result` () {
105
+ every {
106
+ mockkObj.joinBySpace(
107
+ " a" , " b" , " c" , * varargAll { if (position % 2 == 0 ) it == " E" else it == " O" }, " z"
108
+ )
109
+ } returns " Wow, Kotlin rocks!"
110
+
111
+
112
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " O" , " E" , " O" , " E" , " z" )
113
+ assertEquals(" Wow, Kotlin rocks!" , result)
114
+
115
+ assertThrows<MockKException > {
116
+ mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " z" )
117
+ }.also { exception ->
118
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
119
+ }
120
+
121
+ assertThrows<MockKException > {
122
+ mockkObj.joinBySpace(" a" , " b" , " c" , " O" , " Baeldung" , " is" , " cool" , " z" )
123
+ }.also { exception ->
124
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
125
+ }
126
+
127
+ }
128
+
129
+ @Test
130
+ fun `when using varargAny() with position on the mocked object then get expected result` () {
131
+ every {
132
+ mockkObj.joinBySpace(
133
+ " a" , " b" , " c" , * varargAny { if (position % 2 == 0 ) it == " E" else it == " O" }, " z"
134
+ )
135
+ } returns " Wow, Kotlin rocks!"
136
+
137
+ assertThrows<MockKException > {
138
+ mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " z" )
139
+ }.also { exception ->
140
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
141
+ }
142
+
143
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " O" , " Baeldung" , " is" , " cool" , " z" )
144
+ assertEquals(" Wow, Kotlin rocks!" , result)
145
+ }
146
+
147
+ }
0 commit comments