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
+
52
+ @Test
53
+ fun `when using varargAny() on the mocked object then get expected result` () {
54
+ every { mockkObj.joinBySpace(" a" , " b" , " c" , * varargAny { it.startsWith(" d" ) }, " z" ) } returns " Wow, Kotlin rocks!"
55
+
56
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " d1" , " d2" , " d-whatever" , " z" )
57
+ assertEquals(" Wow, Kotlin rocks!" , result)
58
+
59
+ val result2 = mockkObj.joinBySpace(" a" , " b" , " c" , " d1" , " Baeldung" , " z" )
60
+ assertEquals(" Wow, Kotlin rocks!" , result2)
61
+ }
62
+
63
+
64
+ @Test
65
+ fun `when using varargAll() with nArgs on the mocked object then get expected result` () {
66
+ every { mockkObj.joinBySpace(" a" , " b" , " c" , * varargAll { nArgs > 6 }, " z" ) } returns " Wow, Kotlin rocks!"
67
+
68
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " is" , " cool" , " z" )
69
+ assertEquals(" Wow, Kotlin rocks!" , result)
70
+
71
+ assertThrows<MockKException > {
72
+ mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " z" )
73
+ }.also { exception ->
74
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
75
+ }
76
+ }
77
+
78
+ @Test
79
+ fun `when using varargAny() with nArgs on the mocked object then get expected result` () {
80
+ every { mockkObj.joinBySpace(" a" , " b" , " c" , * varargAny { nArgs > 6 }, " z" ) } returns " Wow, Kotlin rocks!"
81
+
82
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " is" , " cool" , " z" )
83
+ assertEquals(" Wow, Kotlin rocks!" , result)
84
+
85
+ assertThrows<MockKException > {
86
+ mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " z" )
87
+ }.also { exception ->
88
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
89
+ }
90
+ }
91
+
92
+
93
+ @Test
94
+ fun `when using varargAll() with position on the mocked object then get expected result` () {
95
+ every {
96
+ mockkObj.joinBySpace(
97
+ " a" , " b" , " c" , * varargAll { if (position % 2 == 0 ) it == " E" else it == " O" }, " z"
98
+ )
99
+ } returns " Wow, Kotlin rocks!"
100
+
101
+
102
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " O" , " E" , " O" , " E" , " z" )
103
+ assertEquals(" Wow, Kotlin rocks!" , result)
104
+
105
+ assertThrows<MockKException > {
106
+ mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " z" )
107
+ }.also { exception ->
108
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
109
+ }
110
+
111
+ assertThrows<MockKException > {
112
+ mockkObj.joinBySpace(" a" , " b" , " c" , " O" , " Baeldung" , " is" , " cool" , " z" )
113
+ }.also { exception ->
114
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
115
+ }
116
+
117
+ }
118
+
119
+ @Test
120
+ fun `when using varargAny() with position on the mocked object then get expected result` () {
121
+ every {
122
+ mockkObj.joinBySpace(
123
+ " a" , " b" , " c" , * varargAny { if (position % 2 == 0 ) it == " E" else it == " O" }, " z"
124
+ )
125
+ } returns " Wow, Kotlin rocks!"
126
+
127
+ assertThrows<MockKException > {
128
+ mockkObj.joinBySpace(" a" , " b" , " c" , " Baeldung" , " z" )
129
+ }.also { exception ->
130
+ assertTrue(exception.message!! .startsWith(" no answer found" ))
131
+ }
132
+
133
+ val result = mockkObj.joinBySpace(" a" , " b" , " c" , " O" , " Baeldung" , " is" , " cool" , " z" )
134
+ assertEquals(" Wow, Kotlin rocks!" , result)
135
+ }
136
+
137
+ }
0 commit comments