Skip to content

Commit 4153a36

Browse files
authored
KTLN-266: add examples of testing lambda function (#590)
1 parent 97b656a commit 4153a36

File tree

4 files changed

+183
-4
lines changed

4 files changed

+183
-4
lines changed

kotlin-testing-2/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</parent>
1515

1616
<dependencies>
17-
<!-- MockK dependencies-->
17+
<!-- MockK dependencies-->
1818
<dependency>
1919
<groupId>io.mockk</groupId>
2020
<artifactId>mockk-jvm</artifactId>

kotlin-testing/pom.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
</parent>
1515

1616
<dependencies>
17-
<dependency>
18-
<groupId>com.nhaarman</groupId>
17+
<dependency>
18+
<groupId>com.nhaarman</groupId>
19+
<artifactId>mockito-kotlin</artifactId>
20+
<version>${nhaarman-mockito-kotlin.version}</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.mockito.kotlin</groupId>
1925
<artifactId>mockito-kotlin</artifactId>
2026
<version>${mockito-kotlin.version}</version>
2127
<scope>test</scope>
@@ -48,7 +54,8 @@
4854
</dependencies>
4955

5056
<properties>
51-
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
57+
<nhaarman-mockito-kotlin.version>1.5.0</nhaarman-mockito-kotlin.version>
58+
<mockito-kotlin.version>5.1.0</mockito-kotlin.version>
5259
<spek.version>1.1.5</spek.version>
5360
<mockk.version>1.9.3</mockk.version>
5461
</properties>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.baeldung.mockito.argumentcaptor
2+
3+
import org.junit.jupiter.api.Test
4+
import org.mockito.kotlin.any
5+
import org.mockito.kotlin.argumentCaptor
6+
import org.mockito.kotlin.eq
7+
import org.mockito.kotlin.mock
8+
import org.mockito.kotlin.times
9+
import org.mockito.kotlin.verify
10+
11+
class CommandExecutor(
12+
private val registry: CommandRegistry,
13+
private val writer: CommandWriter,
14+
) {
15+
fun executeCommand(command: String) {
16+
registry.storeCommand(command) { success ->
17+
success && writer.writeCommand(command)
18+
}
19+
}
20+
}
21+
22+
interface CommandRegistry {
23+
fun storeCommand(command: String, onFinish: (success: Boolean) -> Unit)
24+
}
25+
26+
interface CommandWriter {
27+
fun writeCommand(command: String): Boolean
28+
}
29+
30+
class CommandExecutorUnitTest {
31+
32+
@Test
33+
fun `when command is executed it is stored in registry`() {
34+
// given
35+
val command = "CMD_01"
36+
37+
val registryMock = mock<CommandRegistry>()
38+
val commandExecutor = CommandExecutor(registryMock, mock())
39+
40+
// when
41+
commandExecutor.executeCommand(command)
42+
43+
// then
44+
verify(registryMock).storeCommand(eq(command), any())
45+
}
46+
47+
@Test
48+
fun `when command is executed and registry max size is not reached it is not dumped to a file`() {
49+
// given
50+
val command = "CMD_01"
51+
52+
val registryMock = mock<CommandRegistry>()
53+
val writerMock = mock<CommandWriter>()
54+
val commandExecutor = CommandExecutor(registryMock, writerMock)
55+
56+
// when
57+
commandExecutor.executeCommand(command)
58+
59+
// then
60+
val captor = argumentCaptor<(Boolean) -> Unit>()
61+
62+
verify(registryMock).storeCommand(any(), captor.capture())
63+
64+
val capturedOperation = captor.firstValue
65+
66+
capturedOperation.invoke(false)
67+
verify(writerMock, times(0)).writeCommand(any())
68+
}
69+
70+
@Test
71+
fun `when command is executed and registry max size is reached it is dumped to a file`() {
72+
// given
73+
val command = "CMD_01"
74+
75+
val registryMock = mock<CommandRegistry>()
76+
val writerMock = mock<CommandWriter>()
77+
val commandExecutor = CommandExecutor(registryMock, writerMock)
78+
79+
// when
80+
commandExecutor.executeCommand(command)
81+
82+
// then
83+
val captor = argumentCaptor<(Boolean) -> Unit>()
84+
85+
verify(registryMock).storeCommand(any(), captor.capture())
86+
87+
val capturedOperation = captor.firstValue
88+
89+
capturedOperation.invoke(true)
90+
verify(writerMock, times(1)).writeCommand(any())
91+
}
92+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.mockito.argumentcaptor
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
import org.mockito.kotlin.argumentCaptor
6+
import org.mockito.kotlin.mock
7+
import org.mockito.kotlin.verify
8+
9+
interface MathOperations {
10+
fun performOperation(operation: (a: Int, b: Int) -> Int): Int
11+
}
12+
13+
class CalculatorService(private val mathOperations: MathOperations) {
14+
fun calculate(operation: (a: Int, b: Int) -> Int): Int {
15+
return mathOperations.performOperation(operation)
16+
}
17+
}
18+
19+
class CalculatorServiceUnitTest {
20+
21+
@Test
22+
fun `performs addition operation`() {
23+
// given
24+
val mathOperations = mock<MathOperations>()
25+
val calculatorService = CalculatorService(mathOperations)
26+
27+
// when
28+
calculatorService.calculate { a, b -> a + b }
29+
30+
// then
31+
val lambdaCaptor = argumentCaptor<(Int, Int) -> Int>()
32+
33+
verify(mathOperations).performOperation(lambdaCaptor.capture())
34+
35+
val capturedOperation = lambdaCaptor.firstValue
36+
val operationResult = capturedOperation(11, 3)
37+
38+
assertEquals(14, operationResult)
39+
}
40+
41+
@Test
42+
fun `performs subtraction operation`() {
43+
// given
44+
val mathOperations = mock<MathOperations>()
45+
val calculatorService = CalculatorService(mathOperations)
46+
47+
// when
48+
calculatorService.calculate { a, b -> a - b }
49+
50+
// then
51+
val lambdaCaptor = argumentCaptor<(Int, Int) -> Int>()
52+
53+
verify(mathOperations).performOperation(lambdaCaptor.capture())
54+
55+
val capturedOperation = lambdaCaptor.firstValue
56+
val operationResult = capturedOperation(11, 3)
57+
58+
assertEquals(8, operationResult)
59+
}
60+
61+
@Test
62+
fun `performs multiplication operation`() {
63+
// given
64+
val mathOperations = mock<MathOperations>()
65+
val calculatorService = CalculatorService(mathOperations)
66+
67+
// when
68+
calculatorService.calculate { a, b -> a * b }
69+
70+
// then
71+
val lambdaCaptor = argumentCaptor<(Int, Int) -> Int>()
72+
73+
verify(mathOperations).performOperation(lambdaCaptor.capture())
74+
75+
val capturedOperation = lambdaCaptor.firstValue
76+
val operationResult = capturedOperation(11, 3)
77+
78+
assertEquals(33, operationResult)
79+
}
80+
}

0 commit comments

Comments
 (0)