0
# Boolean Assertions
1
2
Core boolean assertion functions for testing true/false conditions in Kotlin test code. These functions provide both direct value testing and lambda-based conditional testing with optional custom error messages.
3
4
## Capabilities
5
6
### assertTrue
7
8
Asserts that a boolean expression or lambda result is true.
9
10
```kotlin { .api }
11
/**
12
* Asserts that the value is true with an optional message.
13
* @param actual The boolean value to test
14
* @param message Optional message to show if assertion fails
15
*/
16
fun assertTrue(actual: Boolean, message: String? = null)
17
18
/**
19
* Asserts that the given block returns true with an optional message.
20
* @param message Optional message to show if assertion fails
21
* @param block Lambda function that returns a boolean
22
*/
23
inline fun assertTrue(message: String? = null, block: () -> Boolean)
24
```
25
26
**Usage Examples:**
27
28
```kotlin
29
import kotlin.test.assertTrue
30
31
@Test
32
fun testTrueConditions() {
33
// Direct boolean assertion
34
assertTrue(5 > 3)
35
assertTrue(5 > 3, "Five should be greater than three")
36
37
// Lambda-based assertion
38
assertTrue("List should not be empty") {
39
listOf(1, 2, 3).isNotEmpty()
40
}
41
42
// Complex condition testing
43
val user = User("Alice", 25)
44
assertTrue(user.isAdult())
45
assertTrue("User should be adult") { user.age >= 18 }
46
}
47
```
48
49
### assertFalse
50
51
Asserts that a boolean expression or lambda result is false.
52
53
```kotlin { .api }
54
/**
55
* Asserts that the value is false with an optional message.
56
* @param actual The boolean value to test
57
* @param message Optional message to show if assertion fails
58
*/
59
fun assertFalse(actual: Boolean, message: String? = null)
60
61
/**
62
* Asserts that the given block returns false with an optional message.
63
* @param message Optional message to show if assertion fails
64
* @param block Lambda function that returns a boolean
65
*/
66
inline fun assertFalse(message: String? = null, block: () -> Boolean)
67
```
68
69
**Usage Examples:**
70
71
```kotlin
72
import kotlin.test.assertFalse
73
74
@Test
75
fun testFalseConditions() {
76
// Direct boolean assertion
77
assertFalse(3 > 5)
78
assertFalse(3 > 5, "Three should not be greater than five")
79
80
// Lambda-based assertion
81
assertFalse("List should be empty") {
82
emptyList<String>().isNotEmpty()
83
}
84
85
// Complex condition testing
86
val account = Account(balance = 0.0)
87
assertFalse(account.hasPositiveBalance())
88
assertFalse("Account should not be overdrawn") { account.balance < 0 }
89
}
90
```
91
92
## Error Handling
93
94
Both assertion functions will throw an `AssertionError` when the condition fails:
95
96
- **assertTrue**: Throws when the value/block result is false
97
- **assertFalse**: Throws when the value/block result is true
98
99
Custom messages are included in the error to provide context about the failure:
100
101
```kotlin
102
// This will throw: AssertionError: Expected value to be true.
103
assertTrue(false)
104
105
// This will throw: AssertionError: User should be active.
106
assertTrue(user.isActive, "User should be active")
107
```